有字符转换为ASCII码比较简单,将Char类型直接转换为short型即可,例如
short key = Convert.ToInt16("A");
将ASCII码转换为字符,可用如下函数:
//ASC码和字符转换
private 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.");
}
}