JACKY

不自见,故明;不自是,故彰;不自伐,故有功;不自矜;故长;夫唯不争,故天下莫能与之争。
posts - 40, comments - 58, trackbacks - 0, articles - 3
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

C# 字符转ASCII码,ASCII码转字符 [转一下]

Posted on 2009-08-13 23:59 沧海一声笑 阅读(489) 评论(0) 编辑 收藏

 

public static int Asc(string character)
  {
   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.");
   }

  }

ASCII码转字符:

public static string Chr(int asciiCode)
  {
   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
(请您对文章做出评价)