您的位置:首页 > 其它

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();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: