This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24182|Reply: 2

[Source] Batch transcoding of c# version of Unicode character sets to Chinese characters (mutual conversion)

[Copy link]
Posted on 9/7/2015 10:57:18 AM | | |
I saw that many places on the Internet use characters like /u4e2d/u56fd when transmitting Chinese, which are Unicode encoded characters, and I want to know what the specific content is, but it is not easy to see, so I want to decode this character set into normal characters.
At first, I converted the encoding format through Encoding, and found that it didn't work, and I couldn't solve it normally, and then I searched for some similar decoding solutions on the Internet, which were feasible, but I found that it was a bit troublesome to write, and if I had batches of Unicode characters, I couldn't output them directly, and then I looked and looked at them, and finally, I found two methods of char classes: one is char. ConvertFromUtf32, the comment says: Convert the specified Unicode code bit to a UTF-16 encoded string, isn't this just decoding; Another is char. ConvertToUtf32, comment: This method is to convert UTF-16 encoded characters at specified positions in the string into Unicode code points, ha, in fact, it is to convert ordinary characters into Unicode character sets.

  1. /// <summary>  
  2.         /// 把Unicode解码为普通文字  
  3.         /// </summary>  
  4.         /// <param name="unicodeString">要解码的Unicode字符集</param>  
  5.         /// <returns>解码后的字符串</returns>  
  6.         public static string ConvertToGB(string unicodeString)
  7.         {
  8.             string[] strArray = unicodeString.Split(new string[] { @"\u" }, StringSplitOptions.None);
  9.             string result = string.Empty;
  10.             for (int i = 0; i < strArray.Length; i++)
  11.             {
  12.                 if (strArray[i].Trim() == "" || strArray[i].Length < 2 || strArray.Length <= 1)
  13.                 {
  14.                     result += i == 0 ? strArray[i] : @"\u" + strArray[i];
  15.                     continue;
  16.                 }
  17.                 for (int j = strArray[i].Length > 4 ? 4 : strArray[i].Length; j >= 2; j--)
  18.                 {
  19.                     try
  20.                     {
  21.                         result += char.ConvertFromUtf32(Convert.ToInt32(strArray[i].Substring(0, j), 16)) + strArray[i].Substring(j);
  22.                         break;
  23.                     }
  24.                     catch
  25.                     {
  26.                         continue;
  27.                     }
  28.                 }
  29.             }
  30.             return result;
  31.         }

  32.         /// <summary>  
  33.         /// 把汉字字符转码为Unicode字符集  
  34.         /// </summary>  
  35.         /// <param name="strGB">要转码的字符</param>  
  36.         /// <returns>转码后的字符</returns>  
  37.         public static string ConvertToUnicode(string strGB)
  38.         {
  39.             char[] chs = strGB.ToCharArray();
  40.             string result = string.Empty;
  41.             foreach (char c in chs)
  42.             {
  43.                 result += @"\u" + char.ConvertToUtf32(c.ToString(), 0).ToString("x");
  44.             }
  45.             return result;
  46.         }
Copy code






Previous:JS Chinese characters and Unicode encoding are exchanged Unicode encryption Unicode decryption
Next:How do I do port mapping on a Cisco router?
Posted on 2/19/2016 11:12:53 PM |
Posted on 11/12/2022 1:53:27 PM |
Reply to the post to express your gratitude
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com