您的位置:首页 > 编程语言 > C#

C# ASCII码与字符之间相互转化

2014-05-25 22:04 148 查看
public  int Asc(string character) /*字符转化为ASCII*/
          {
           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.");
           }

          }

        public  string Chr(int asciiCode) /*ASCII 转化为 字符*/
          {
           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.");
           }
          }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: