C# ASCII码字符转换
生活随笔
收集整理的這篇文章主要介紹了
C# ASCII码字符转换
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C#單純的字母數(shù)字ASCII碼轉(zhuǎn)換
字母轉(zhuǎn)換成數(shù)字
byte[] array = new byte[1]; //定義一組數(shù)組array array = System.Text.Encoding.ASCII.GetBytes(string); //string為待轉(zhuǎn)換的字母 int asciicode = (short)(array[0]); //asciicode 為整數(shù)ASCII碼 ASCII碼 = Convert.ToString(asciicode); //將轉(zhuǎn)換一的ASCII碼轉(zhuǎn)換成string型數(shù)字轉(zhuǎn)換成字母(1)
byte[] array = new byte[1]; array[0] = (byte)(Convert.ToInt32(ASCII碼)); //ASCII碼強(qiáng)制轉(zhuǎn)換二進(jìn)制 string str=Convert.ToString(System.Text.Encoding.ASCII.GetString(array));//str為ASCII碼對應(yīng)的字符數(shù)字轉(zhuǎn)換成字母(2)
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); byte[] byteArray = new byte[] { (byte)ASCII碼 }; string str = asciiEncoding.GetString(byteArray);實(shí)例:
string sVer = ""; string strCheck = "10C"; string sLetter = strCheck.Substring(2, 1); // 獲取strCheck第三位的字符// 獲取sLetter資訊的Ascii碼 byte[] array = new byte[1]; //定義一組數(shù)組array array = System.Text.Encoding.ASCII.GetBytes(sLetter); //string轉(zhuǎn)換的字母 int nAsciicode = (short)(array[0]);// 判斷是不是大寫字母 if (nAsciicode <= 65 || nAsciicode >= 90) continue;// 判斷sLetter字母前的所有大寫字母 for (int k = 65; k < nAsciicode; k++) {// 將Ascii碼轉(zhuǎn)化為大寫字母System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();byte[] byteArray = new byte[] { (byte)k };string strCharacter = asciiEncoding.GetString(byteArray);// 拼接版本.if (sVer == ""){if (strCheck.Length == 3){sVer = strCheck.Substring(0, 2) + strCharacter ;}else{sVer = strCheck.Substring(0, 2) + strCharacter + strCheck.Substring(3, strCheck.Length - 3);}}else{if (strCheck.Length == 3){sVer += "," + strCheck.Substring(0, 2) + strCharacter;}else{sVer += "," + strCheck.Substring(0, 2) + strCharacter + strCheck.Substring(3, strCheck.Length - 3);}}}上面實(shí)例運(yùn)行結(jié)果應(yīng)為:10A,10B
千里之行始于足下,原共勉之!
總結(jié)
以上是生活随笔為你收集整理的C# ASCII码字符转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle 异常处理函数SQLCODE
- 下一篇: 拉扎维RFIC总结与笔记