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

来源:互联网 发布:家装网络推广 编辑:程序博客网 时间:2024/05/16 23:42

 

 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.");           }          }


 

0 0
原创粉丝点击