如何將英文字母大寫”A”轉換為”65”與如何將”65”轉換為英文字母大寫的”A”。
下列範例中RetAsciiCode將大寫”A”轉換成”65”後return給呼叫RetAsciiCode的原程序變數,另外RetAsciiChar帶入一個數字(asciiCode)例如”65”,轉換成大寫”A”並將結果return給呼叫的原程序變數。
C#:
- int intAsciiCode = Int32.Parse(RetAsciiCode(“A”)); //回傳數字65
- string strAsciiChar = RetAsciiChar(65); //回傳大寫A
- public int RetAsciiCode(string MyString)
- {
- if (MyString.Length == 0)
- return 0;
- else if (MyString.Length > 1)
- MyString = MyString[0].ToString();
- int AsciiCodeO = (int)System.Convert.ToChar(MyString);
- byte[] AsciiCodeB = System.Text.Encoding.ASCII.GetBytes(MyString);
- //int AsciiCode = System.Convert.ToInt32(AsciiCodeB);
- return AsciiCodeO;
- }
- public string RetAsciiChar(int AsciiCode)
- {
- return System.Convert.ToChar(AsciiCode).ToString();
- }
沒有留言:
張貼留言