csharp: string Encoding

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/// <summary>
      /// 中文转unicode
      /// </summary>
      /// <param name="str"></param>
      /// <returns></returns>
      public static string unicode_0(string str)
      {
          string outStr = "";
          if (!string.IsNullOrEmpty(str))
          {
              for (int i = 0; i < str.Length; i++)
              {
                  outStr += "/u" + ((int)str[i]).ToString("x");
              }
          }
          return outStr;
      }
      /// <summary>
      /// 汉字转为Unicode编码
      /// </summary>
      /// <param name="str"></param>
      /// <returns></returns>
      public static string bgktounicode(string str)
      {
          string outstr = "";
          //汉字转为Unicode编码:
          string hz = str;
          byte[] b = Encoding.Unicode.GetBytes(hz);
          string o = "";
          foreach (var x in b)
          {
              o += string.Format("{0:X2}", x) + " ";
          }
          outstr = o;
          return outstr;
      }
 
      /// <summary>
      ///  unicode转中文
      /// </summary>
      /// <param name="str"></param>
      /// <returns></returns>
      public static string unicode_1(string str)
      {
          string outStr = "";
          if (!string.IsNullOrEmpty(str))
          {
              string[] strlist = str.Replace("/", "").Split('u');
              try
              {
                  for (int i = 1; i < strlist.Length; i++)
                  {
                      //将unicode字符转为10进制整数,然后转为char中文字符 
                      outStr += (char)int.Parse(strlist[i], System.Globalization.NumberStyles.HexNumber);
                  }
              }
              catch (FormatException ex)
              {
                  outStr = ex.Message;
              }
          }
          return outStr;
      }
      /// <summary>
      /// unicode转中文(符合js规则的)
      /// </summary>
      /// <param name="str"></param>
      /// <returns></returns>
      public static string unicode_js_1(string str)
      {
          string outStr = "";
          Regex reg = new Regex(@"(?i)\\u([0-9a-f]{4})");
          outStr = reg.Replace(str, delegate(Match m1)
          {
              return ((char)Convert.ToInt32(m1.Groups[1].Value, 16)).ToString();
          });
          return outStr;
      }
      /// <summary>
      /// 中文转unicode(符合js规则的)
      /// </summary>
      /// <param name="str"></param>
      /// <returns></returns>
      public static string unicode_js_0(string str)
      {
          string outStr = "";
          string a = "";
          if (!string.IsNullOrEmpty(str))
          {
              for (int i = 0; i < str.Length; i++)
              {
                  if (Regex.IsMatch(str[i].ToString(), @"[\u4e00-\u9fa5]")) { outStr += "\\u" + ((int)str[i]).ToString("x"); }
                  else { outStr += str[i]; }
              }
          }
          return outStr;
      }
 
      /// <summary>
      /// 骞垮憡涓戦椈
      /// </summary>
      /// <param name="utf8String"></param>
      /// <returns></returns>
      public static string unicodeTogbk(string utf8String)
      {
          string defaultString = "";
          Encoding utf8 = Encoding.UTF8;
          Encoding defaultCode = Encoding.Default;
          // Convert the string into a byte[].
          byte[] utf8Bytes = Encoding.Default.GetBytes(utf8String);
          // Perform the conversion from one encoding to the other.
          byte[] defaultBytes = Encoding.Convert(utf8, defaultCode, utf8Bytes);
          // Convert the new byte[] into a char[] and then into a string.
          // This is a slightly different approach to converting to illustrate
          // the use of GetCharCount/GetChars.
          char[] defaultChars = new char[defaultCode.GetCharCount(defaultBytes, 0, defaultBytes.Length)];
          defaultCode.GetChars(defaultBytes, 0, defaultBytes.Length, defaultChars, 0);
          defaultString = new string(defaultChars);
          return defaultString;
      }
      /// <summary>
      /// 骞垮憡涓戦椈
      /// </summary>
      /// <param name="utf8String"></param>
      /// <returns></returns>
      public static string unicodeTogbkb(string utf8String)
      {
          string strBuffer = "";
          byte[] buffer1 = Encoding.Default.GetBytes(utf8String);
          byte[] buffer2 = Encoding.Convert(Encoding.UTF8, Encoding.Default, buffer1, 0, buffer1.Length);
          strBuffer = Encoding.Default.GetString(buffer2, 0, buffer2.Length);
          return strBuffer;
      }

 

posted @   ®Geovin Du Dream Park™  阅读(1557)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示