首先转换方面:
1. byte[]
2. bytes = System.Text.Encoding.UTF8.GetBytes(msgStr);
3. bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, bytes);
4. bytes = System.Text.Encoding.Unicode.GetBytes(msgStr);
5. msgStr = System.Text.Encoding.GetEncoding("gb2312").GetString(Encoding.Convert(Encoding.Unicode, Encoding.GetEncoding("gb2312"), bytes));
编码:"\\uxxxx" 转换为"\uxxxx"
解码:"\uxxxx" 转换为"\\uxxxxx"
编码算法
1. //UNICODE字符转为中文
2. 对这个方法做一点改进 使他支持中英混排
3. public static string ConvertUnicodeStringToChinese(string unicodeString)
4. {
5. if (string.IsNullOrEmpty(unicodeString))
6. return string.Empty;
7.
8. string outStr = unicodeString;
9.
10. Regex re = new Regex("\\\\u[0123456789abcdef]{4}", RegexOptions.IgnoreCase);
11. MatchCollection mc = re.Matches(unicodeString);
12. foreach (Match ma in mc)
13. {
14. outStr = outStr.Replace(ma.Value, ConverUnicodeStringToChar(ma.Value).ToString());
15. }
16. return outStr;
17. }
18.
19. private static char ConverUnicodeStringToChar(string str)
20. {
21. char outStr = Char.MinValue;
22. char)int.Parse(str.Remove(0, 2), System.Globalization.NumberStyles.HexNumber);
23. return outStr;
24. }
解码:
1. static Regex reUnicode = new Regex(@"\\u([0-9a-fA-F]{4})", RegexOptions.Compiled);标签:ma,string,Encoding,C#,解码,bytes,unicode,outStr,unicodeString From: https://blog.51cto.com/u_15834343/5887807
2. public static string ConvertChineseToUnicodeString(string chinese)
3. {
4. MatchCollection mc = re.Matches(chinese);
5. string unicodeString="";
6. foreach(Match ma in mc)
7. {
8. char c;
9. if(Short.TryParse(ma.Remove(0,1), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture, out c))
10. {
11. "\\u"+c);
12. }
13. }
14. return unicodeString;
15. }