UTF8字符串转换为汉字 c#

 

 

  1 using System;
  2 
  3 
  4 
  5 /// <summary>
  6 /// UTF8字符串转换为汉字用的类
  7 /// 转换如"\\u8d35"之类的字符串为对应的汉字
  8 /// </summary>
  9 class UTF8String
 10 {
 11     string m_strContent = "";
 12     /// <summary>
 13     /// 构造函数
 14     /// </summary>
 15     /// <param name="content">要转换的字符串</param>
 16     public UTF8String(string content)
 17     {
 18         m_strContent = content;
 19     }
 20     public string getContent()
 21     {
 22         return m_strContent;
 23     }
 24     /// <summary>
 25     /// 转换函数
 26     /// </summary>
 27     /// <returns>返回转换好的字符串</returns>
 28     public string ToString()
 29     {
 30         string reString = null;
 31         char[] content = m_strContent.ToCharArray(); //把字符串变为字符数组,以进行处理
 32         for (int i = 0; i < content.Length; i++) //遍历所有字符
 33         {
 34             if (content[i] == '\\') //判断是否转义字符 \ 
 35             {
 36                 switch (content[i + 1]) //判断转义字符的下一个字符是什么
 37                 {
 38                     case 'u': //转换的是汉字
 39                     case 'U':
 40                         reString += HexArrayToChar(content, i + 2); //获取对应的汉字
 41                         i = i + 5;
 42                         break;
 43                     case '/': //转换的是 /
 44                     case '\\': //转换的是 \
 45                     case '"':
 46                         break;
 47                     default: //其它
 48                         reString += EscapeCharacter(content[i + 1]); //转为其它类型字符
 49                         i = i + 1;
 50                         break;
 51                 }
 52             }
 53             else
 54                 reString += content[i]; //非转义字符则直接加入
 55         }
 56         return reString;
 57     }
 58 
 59     /// <summary>
 60     /// 字符数组转对应汉字字符
 61     /// </summary>
 62     /// <param name="content">要转换的数字</param>
 63     /// <param name="startIndex">起始位置</param>
 64     /// <returns>对应的汉字</returns>
 65     private char HexArrayToChar(char[] content, int startIndex)
 66     {
 67         char[] ac = new char[4];
 68         for (int i = 0; i < 4; i++) //获取要转换的部分
 69             ac[i] = content[startIndex + i];
 70         string num = new string(ac); //字符数组转为字符串
 71         return HexStringToChar(num);
 72     }
 73 
 74     /// <summary>
 75     /// 转义字符转换函数
 76     /// 转换字符为对应的转义字符
 77     /// </summary>
 78     /// <param name="c">要转的字符</param>
 79     /// <returns>对应的转义字符</returns>
 80     private char EscapeCharacter(char c)
 81     {
 82         char rc;
 83         switch (c)
 84         {
 85             case 't':
 86                 c = '\t';
 87                 break;
 88             case 'n':
 89                 c = '\n';
 90                 break;
 91             case 'r':
 92                 c = '\r';
 93                 break;
 94             case '\'':
 95                 c = '\'';
 96                 break;
 97             case '0':
 98                 c = '\0';
 99                 break;
100         }
101         return c;
102     }
103 
104     /// <summary>
105     /// 字符串转对应汉字字符
106     /// 只能处理如"8d34"之类的数字字符为对应的汉字
107     /// 例子:"9648" 转为 '陈'
108     /// </summary>
109     /// <param name="content">转换的字符串</param>
110     /// <returns>对应的汉字</returns>
111     public static char HexStringToChar(string content)
112     {
113         int num = Convert.ToInt32(content, 16);
114         return (char)num;
115     }
116 
117     /// <summary>
118     /// 把string转为UTF8String类型
119     /// </summary>
120     /// <param name="content"></param>
121     /// <returns></returns>
122     public static UTF8String ValueOf(string content)
123     {
124         string reString = null;
125         char[] ac = content.ToCharArray();
126         int num;
127         foreach (char c in ac)
128         {
129             num = (int)c;
130             string n = num.ToString("X2");
131             if (n.Length == 4)
132                 reString += "\\u" + n;
133             else
134                 reString += c;
135         }
136         return new UTF8String(reString);
137     }
138 
139 }

 

posted on 2014-09-22 16:41  游戏开发:主席  阅读(4048)  评论(0编辑  收藏  举报

导航