博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中文和UNICODE字符转换方法
阅读量:6853 次
发布时间:2019-06-26

本文共 1736 字,大约阅读时间需要 5 分钟。

1         ///  2         /// 将Unicode编码转换为汉字字符串 3         ///  4         /// Unicode编码字符串 5         /// 
汉字字符串
6 public static string ToGB2312(string str) 7 { 8 StringBuilder sb = new StringBuilder(); 9 MatchCollection mCollection2 = Regex.Matches(str, "([\\w]+)|(\\\\u([\\w]{4}))");10 if (mCollection2 != null && mCollection2.Count > 0)11 { 12 foreach (Match m2 in mCollection2)13 {14 string v = m2.Value;15 if (v.StartsWith("\\u"))16 {17 string word = v.Substring(2);18 byte[] codes = new byte[2];19 int code = System.Convert.ToInt32(word.Substring(0, 2), 16);20 int code2 = System.Convert.ToInt32(word.Substring(2), 16);21 codes[0] = (byte)code2;22 codes[1] = (byte)code;23 sb.Append(Encoding.Unicode.GetString(codes));24 }25 else26 {27 sb.Append(v);28 }29 } 30 }31 return sb.ToString();32 }
1         //可以包括其他字符        2         public string uncode(string str) 3         { 4             string outStr = ""; 5             Regex reg = new Regex(@"(?i)//u([0-9a-f]{4})"); 6             outStr = reg.Replace(str, delegate(Match m1) 7             { 8                 return ((char)Convert.ToInt32(m1.Groups[1].Value, 16)).ToString(); 9             });10             return outStr;11         }

 

 

转载于:https://www.cnblogs.com/tomsense/p/3939794.html

你可能感兴趣的文章
[LeetCode] Remove Duplicates from Sorted Array II
查看>>
【深度学习笔记1】如何建立和确定模型正确性?如何优化模型?
查看>>
Collection集合家族
查看>>
RtlWerpReportException failed with status code :-1073741823
查看>>
5-2 类型转换 @SuppressWarnings("unchecked")
查看>>
实验 5 编写、调试具有多个段的程序
查看>>
Verilog代码可移植性设计(转自特权同学博客http://bbs.ednchina.com/BLOG_ARTICLE_1983188.HTM)...
查看>>
浅析Linux网络子系统(三)
查看>>
jquery.validate ajax验证
查看>>
【风马一族_物理】维度空间的粒子
查看>>
手把手教你如何把java代码,打包成jar文件以及转换为exe可执行文件
查看>>
Codeforces Round #363 Fix a Tree(树 拓扑排序)
查看>>
hihocoder1455 Rikka with Tree III(bitset 莫队 dfs序)
查看>>
SQL Server 2008中的MERGE(不仅仅是合并)
查看>>
啤酒与饮料算法
查看>>
xxx is not in the sudoers file.This incident will be reported.的解决方法
查看>>
Java实现冒泡排序、折半查找
查看>>
[C++] 引用
查看>>
Drupal7 Module chapter 1 (猪扒7的开发)第一章
查看>>
Django - admin管理工具
查看>>