//字符转ASCII码:
public static int Asc(string character)
{
if (character.Length == 1)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
return (intAsciiCode);
}
else
{
throw new Exception("Character is not valid.");
}
}
ASCII码转字符:
public static string Chr(int asciiCode)
{
if (asciiCode >= 0 && asciiCode <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)asciiCode };
string strCharacter = asciiEncoding.GetString(byteArray);
return (strCharacter);
}
else
{
throw new Exception("ASCII Code is not valid.");
}
}
//另一种写法
string str="abcd";
byte[] bytetest = System.Text.Encoding.Default.GetBytes(str.ToString());
C#字符串和ASCII码的转换
免责声明:文章转载自《C#字符串和ASCII码的转换》仅用于学习参考。如对内容有疑问,请及时联系本站处理。
上篇Mac版sublime text右键open in browser 不能识别中文名解决办法npm库使用roullup封装经验总结下篇
宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=