Unicode转换成中文明文字符串,条码枪扫描结果解析
2015-11-06 20:41
483 查看
Unicode转换成中文字符串,条码枪扫描结果解析。
需求,二维条码枪扫描的结果为Unicode字符串,需要解释成明文。
解析textbox.text中的Unicode字符串成明文,支持Unicode转换成汉字。
如把:\u6211\u662f\u5171\u26\u58\u6c\u30\u30\u31转换成明文串。
原文:
/article/5295478.html
public static string ToUnicodeString(this string str)
{
StringBuilder strResult = new StringBuilder();
if (!string.IsNullOrEmpty(str))
{
for (int i = 0; i < str.Length; i++)
{
strResult.Append("\\u");
strResult.Append(((int)str[i]).ToString("x"));
}
}
return strResult.ToString();
}
public static string FromUnicodeString(this string str)
{
//最直接的方法Regex.Unescape(str);
StringBuilder strResult = new StringBuilder();
if (!string.IsNullOrEmpty(str))
{
string[] strlist = str.Replace("\\", "").Split('u');
try
{
for (int i = 1; i < strlist.Length; i++)
{
int charCode = Convert.ToInt32(strlist[i], 16);
strResult.Append((char)charCode);
}
}
catch (FormatException ex)
{
return Regex.Unescape(str);
}
}
return strResult.ToString();
}
--测试
string str = "我是共.产.党";
string unicodeStr = str.ToUnicodeString();
string chStr = unicodeStr.FromUnicodeString();
Unicode转换成中文字符串,条码枪扫描结果解析。
需求,二维条码枪扫描的结果为Unicode字符串,需要解释成明文。
解析textbox.text中的Unicode字符串成明文,支持Unicode转换成汉字。
如把:\u6211\u662f\u5171\u26\u58\u6c\u30\u30\u31转换成明文串。
原文:
/article/5295478.html
public static string ToUnicodeString(this string str)
{
StringBuilder strResult = new StringBuilder();
if (!string.IsNullOrEmpty(str))
{
for (int i = 0; i < str.Length; i++)
{
strResult.Append("\\u");
strResult.Append(((int)str[i]).ToString("x"));
}
}
return strResult.ToString();
}
public static string FromUnicodeString(this string str)
{
//最直接的方法Regex.Unescape(str);
StringBuilder strResult = new StringBuilder();
if (!string.IsNullOrEmpty(str))
{
string[] strlist = str.Replace("\\", "").Split('u');
try
{
for (int i = 1; i < strlist.Length; i++)
{
int charCode = Convert.ToInt32(strlist[i], 16);
strResult.Append((char)charCode);
}
}
catch (FormatException ex)
{
return Regex.Unescape(str);
}
}
return strResult.ToString();
}
--测试
string str = "我是共.产.党";
string unicodeStr = str.ToUnicodeString();
string chStr = unicodeStr.FromUnicodeString();
相关文章推荐
- React学习笔记---项目构建
- 猪八戒网站上的骗子为什么这么多
- Android Studio1.3/1.4如何升级Gradle到version 2.5
- 抽象工厂+反射+依赖注入 实现对数据访问层和业务逻辑层的优化
- 论文笔记《Fully Convolutional Networks for Semantic Segmentation》
- CodeForces 319B 栈
- hdu2033
- 快速排序的简单实现(Quick_Sort)
- 静态编联与动态编联总结
- SpringMVC 学习系列 (7) 之 格式化显示
- codeforces Searching for Graph
- 如何发表高水平论文 (转载)
- 快速排序的简单实现(Quick_Sort)
- Java Axis2 1.6.3+JDK1.7.0_13+Tomcat7.0.65+eclipse搭建web service
- SpringMVC 学习系列 (6) 之 数据验证
- 你必须知道的.NET之特性和属性
- KMP模板
- 关于手机屏幕信息的获取
- 13 Roman to Integer
- SpringMVC 学习系列 (5) 之 数据绑定 -2