C#字符串与unicode互相转换.
1. /// <summary>
2. /// 普通字符串与UniCode字符串间转换
3. /// </summary>
4. public static class StringToUniCode
5. {
6. /// <summary>
7. /// 字符串转为UniCode码字符串
8. /// </summary>
9. /// <param name="s"></param>
10. /// <returns></returns>
11. public static string StringToUnicode(string s)
12. {
13. char[] charbuffers = s.ToCharArray();
14. byte[] buffer;
15. StringBuilder sb = new StringBuilder();
16. for (int i = 0; i < charbuffers.Length; i++)
17. {
18. buffer = System.Text.Encoding.Unicode.GetBytes(charbuffers[i].ToString());
19. sb.Append(String.Format("//u{0:X2}{1:X2}", buffer[1], buffer[0]));
20. }
21. return sb.ToString();
22. }
23. /// <summary>
24. /// Unicode字符串转为正常字符串
25. /// </summary>
26. /// <param name="srcText"></param>
27. /// <returns></returns>
28. public static string UnicodeToString(string srcText)
29. {
30. string dst = "";
31. string src = srcText;
32. int len = srcText.Length / 6;
33. for (int i = 0; i <= len - 1; i++)
34. {
35. string str = "";
36. str = src.Substring(0, 6).Substring(2);
37. src = src.Substring(6);
38. byte[] bytes = new byte[2];
39. bytes[1] = byte.Parse(int.Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
40. bytes[0] = byte.Parse(int.Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString());
41. dst += Encoding.Unicode.GetString(bytes);
unicode所有字符42. }
43. return dst;
44. }
45. }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论