C a simple way to convert Unicode encodings into Chinese character strings

  • 2020-05-12 02:58:42
  • OfStack

C# converts UNICODE in js into a string, which can't be found on the Internet. If you encounter a number, you can't get it out.

Examples are as follows:


///  will Unicode The encoding is converted to a Chinese character string  
    /// 
    /// Unicode Encoded string  
    ///  Chinese character string  
    public static string ToGB2312(string str) 
    { 
      MatchCollection mc = Regex.Matches(str, "([\\w]+)|(\\\\u([\\w]{4}))");
      if (mc != null && mc.Count > 0)
      {
        StringBuilder sb = new StringBuilder();
        foreach (Match m2 in mc)
        {
          string v = m2.Value;
          if (v.StartsWith("\\"))
          {
            string word = v.Substring(2);
            byte[] codes = new byte[2];
            int code = Convert.ToInt32(word.Substring(0, 2), 16);
            int code2 = Convert.ToInt32(word.Substring(2), 16);
            codes[0] = (byte)code2;
            codes[1] = (byte)code;
            sb.Append(Encoding.Unicode.GetString(codes));
          }
          else
          {
            sb.Append(v);
          }
        }
        return sb.ToString();
      }
      else
      {
        return str;
      }
    }

Related articles: