String类型与ASCII码的互换⼀、字符串转换为ASCII:
1)把字符(串)直接转换为int类型,即可得到ASCII码;
2)再将数字转换为字符串转出;
//把字符转换为ASCII
//⽅法⼀:
int asc = (int)'a';
Console.WriteLine(asc.ToString());
//⽅法⼆:
string str = "a";
byte[] bytes= System.Text.Encoding.ASCII.GetBytes(str);
int arr = (int)bytes[0];
Console.WriteLine(arr.ToString());
//把字符串转换为ASCII
String str1 = "wslkx";
char[] char1 = str1.ToCharArray();
//byte[] char1 = System.Text.Encoding.ASCII.GetBytes(str1);
string conside = null;
for(int i = 0; i < char1.Length; i++)
{
int asc1 = (int)(char1[i]);
conside += asc1.ToString()+",";
Console.WriteLine(conside);
}
Console.WriteLine(conside);
⼆、ASCII码转换为字符。
//单个字符
string alph = ((char)65).ToString();
Console.WriteLine(alph);
//多位字符
string[] num = { "65", "66", "67", "68", "69" };
string alph1 = null;
for(int i = 0; i < num.Length; i++)
{
string str3 = ((char)Int32.Parse(num[i])).ToString();//需要将string类型转换为int类型
alph1 += str3 + ",";
}
c++string类型Console.WriteLine(alph1);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论