c#中文转unicode字符示例分享
//可以包括其他字符
public string uncode(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;
}
//中文转为UNICODE字符
string str = "中文";
string outStr = "";
if (!string.IsNullOrEmpty(str))
{
for (int i = 0; i < str.Length; i++)
{
//将中文字符转为10进制整数,然后转为16进制unicode字符
outStr += "//u" + ((int)str[i]).ToString("x");
}
}
//UNICODE字符转为中文
string str = "//u4e2d//u6587";
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;
}
}
上一篇:Entity Framework使用DataBase First模式实现数据库的增删改查
栏 目:.NET代码
本文标题:c#中文转unicode字符示例分享
本文地址:http://www.codeinn.net/misctech/217348.html