示例一
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace UnicodeTest
{
class Program
{
static void Main(string[] args)
{
string s = "this is a 中文字符串! to unicode.";
string ts = CNStringToUnicode(s);
string os = Regex.Unescape(ts);
Console.WriteLine("原字符串:"+s);
Console.WriteLine("转换后的字符串:"+ts);
Console.WriteLine("还原字符串:"+os);
Console.Read();
}
//仅把字符串中的中文转成unicode其它字符保持不变
public static string CNStringToUnicode(string s)
{
string uni_str = "";
for (int l = 0; l < s.Length; l++)
{
int c = s[l];
if (c > 255)
{
uni_str += "\\u" + c.ToString("x").ToUpper();
}
else
{
uni_str += (char)c;
}
}
return uni_str;
}
}
}
运行测试