![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
//
字符转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());
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());