字符转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.");
}
}
本文提供了两个实用的方法:将字符转换为ASCII码和将ASCII码转换回字符。这两个方法使用.NET Framework中的System.Text.ASCIIEncoding类实现,确保了转换的准确性。文章通过具体的代码示例展示了如何进行这两种转换。
16万+

被折叠的 条评论
为什么被折叠?



