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

时间:2025-02-28 20:03:35

 

 public  int Asc(string character) /*字符转化为ASCII*/
          {
           if ( == 1)
           {
             asciiEncoding = new ();
            int intAsciiCode = (int)(character)[0];
            return (intAsciiCode);
           }
           else
           {
            throw new Exception("Character is not valid.");
           }

          }


        public  string Chr(int asciiCode) /*ASCII 转化为 字符*/
          {
           if (asciiCode >= 0 && asciiCode <= 255)
           {
                 asciiEncoding = new ();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                 string strCharacter = (byteArray);
                 return (strCharacter);
           }
           else
           {
            throw new Exception("ASCII Code is not valid.");
           }
          }