比较两个char的ASCII码大小(1:大于 0:等于 -1:小于)
private int CompAsc (char a, char b)
{
return (short )a>(short )b?1 :((short )a==(short )b?0 :-1 );
}
字符转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." );
}
}