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 }
相關文章
相關標籤/搜索