百度空间 | 百度首页 
 
查看文章
 
C# 字符转ASCII码,ASCII码转字符
2008-08-28 11:00

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)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu