menu 首页 标签 归档 视频 关于
CSharp-Unicode与汉字互转

一言加载中...

public string UnicodeToString(string unicode)
{
    string result = "";
    //正则匹配
    Regex reg = new Regex(@"(?i)//u([0-9a-f]{4})");
    result = reg.Replace(unicode, delegate (Match m)
    {
        return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString();
    });
    return result;
}

2.将汉字转话为Unicode

public static string StringToUnicode(string value)
{
    byte[] bytes = Encoding.Unicode.GetBytes(value);
    StringBuilder str= new StringBuilder();
    for (int i = 0; i < bytes.Length; i += 2)
    &#123;
        // 取两个字符,每个字符都是右对齐。
        str.AppendFormat("u&#123;0&#125;&#123;1&#125;", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
    &#125;
    return str.ToString();
&#125;

TonyChen
2018.6.11
Sometimes you got to run before you can walk———-先做再说

写博客不易,请我喝杯咖啡?

评论

arrow_upward