C#實現(xiàn)ASCII和字符串相互轉換的代碼示例
更新時間:2026年01月01日 11:51:43 作者:yue008
在C#編程語言中,ASCII碼是一種廣泛使用的字符編碼標準,它將128個不同的字符與7位的二進制數(shù)字對應起來,在處理文本時,我們有時需要將ASCII碼與字符串進行相互轉換,以下是如何在C#中實現(xiàn)這一操作的詳細說明,需要的朋友可以參考下
知識點
string
Stirng.Empty
- 表示空字符串。 此字段為只讀。此字段的值為零長度字符串“”。
- string為引用數(shù)據(jù)類型。會在內(nèi)存的棧和堆上分配存儲空間。因此string.Empty與“”都會在棧上保存一個地址,這個地址占4字節(jié),指向內(nèi)存堆中的某個長度為0的空間,這個空間保存的是string.Empty的實際值。
- 區(qū)別于null,null 只在棧上分配了空間,在堆上沒有分配
out
可以在兩個上下文中使用 out 關鍵字:
- 作為 參數(shù)修飾符,通過引用而不是值將參數(shù)傳遞給方法。
- 在接口和委托的 泛型類型參數(shù)聲明中 ,該聲明指定類型參數(shù)是協(xié)變的。
- 當方法需要返回多個值時,關鍵字 out 特別有用,因為可以使用多個 out 參數(shù)。
public void Main()
{
double radiusValue = 3.92781;
//Calculate the circumference and area of a circle, returning the results to Main().
CalculateCircumferenceAndArea(radiusValue, out double circumferenceResult, out var areaResult);
System.Console.WriteLine($"Circumference of a circle with a radius of {radiusValue} is {circumferenceResult}.");
System.Console.WriteLine($"Area of a circle with a radius of {radiusValue} is {areaResult}.");
Console.ReadLine();
}
//The calculation worker method.
public static void CalculateCircumferenceAndArea(double radius, out double circumference, out double area)
{
circumference = 2 * Math.PI * radius;
area = Math.PI * (radius * radius);
}
Encoding
將字符串從一種編碼轉換為另一種編碼,
編碼(Encoding):將 Unicode 字符轉換為字節(jié)序列的過程。
解碼(Decoding):將字節(jié)序列轉換回 Unicode 字符的過程。
方法
- GetEncoding。返回指定代碼頁的編碼。屬于編碼過程
//返回與指定代碼頁名稱關聯(lián)的編碼。
//Encoding.GetEncoding(String)
using System;
using System.Text;
public class SamplesEncoding {
public static void Main() {
// Get a UTF-32 encoding by codepage.
Encoding e1 = Encoding.GetEncoding( 12000 );
// Get a UTF-32 encoding by name.
Encoding e2 = Encoding.GetEncoding( "utf-32" );
// Check their equality.
Console.WriteLine( "e1 equals e2? {0}", e1.Equals( e2 ) );
}
}
/*
This code produces the following output.
e1 equals e2? True
*/
編碼表

常用編碼格式:

- GetBytes
在派生類中重寫時,將一組字符編碼為一個字節(jié)序列。屬于解碼過程
//GetBytes(Char[]) //在派生類中重寫時,將指定字符數(shù)組中的所有字符編碼為一個字節(jié)序列。
字符串轉換為數(shù)組
字符串可以理解為在字符數(shù)組。所以對手winform控件textbox的text值,也可以作為字符數(shù)組,
if(textBox1.Text!="")
{
string chars_StrA = "hello";
string chars_StrB=string.Empty;
char[] txtChars01 = new char[10];
char[] txtChars02 = new char[10];
for (int i=0;i<textBox1.Text.Length;i++)
{
txtChars01[i] =textBox1.Text[i];
}
for(int j= 0; j< chars_StrA.Length; j++)
{
txtChars02[j] = chars_StrA[j];
chars_StrB += txtChars02[j];
}
}
代碼

private void btn_ToASCII_Click(object sender, EventArgs e)
{
if(txt_char.Text!=string.Empty)
{
if(Encoding.GetEncoding("unicode").GetBytes(new char[] { txt_char.Text[0] })[1]==0)
{
txt_ASCII.Text = Encoding.GetEncoding("unicode").GetBytes(txt_char.Text)[0].ToString();
}
else
{
txt_ASCII.Text = string.Empty;
MessageBox.Show("請輸入字母!", "提示!");
}
}
}
private void btn_ToChar_Click(object sender, EventArgs e)
{
if(txt_ASCII2.Text!=string.Empty)
{
int P_int_Num;//定義整型局部變量
if(int.TryParse(txt_ASCII2.Text,out P_int_Num))
{
txt_Char2.Text = ((char)P_int_Num).ToString();
}
else
{
MessageBox.Show("請輸入正確ASCII碼值", "錯誤");
}
}
}
到此這篇關于C#實現(xiàn)ASCII和字符串相互轉換的代碼示例的文章就介紹到這了,更多相關C# ASCII和字符串互轉內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
c# 使用谷歌身份驗證GoogleAuthenticator的示例
這篇文章主要介紹了c# 使用谷歌身份驗證GoogleAuthenticator的示例,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01

