c#字符型转化为asc_C#字符串和ASCII码的转换
//字符轉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#字符型转化为asc_C#字符串和ASCII码的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 通过计算机网络进行的商务活动包括,电子商
- 下一篇: 自学html和css,学习HTML和CS