UnicodeToGB2312

http://www.myluoluo.com/unicodetogb2312.love

 

你是否遇到类似于:\u5355\u4f4d之类的让人纠结的字符?

一个JS文件中一堆一堆的全都是这种,分析起来很吃力?

 

/// <summary>
/// 将Unicode编码转换为汉字字符串
/// </summary>
/// <param name="str">Unicode编码字符串</param>
/// <returns>汉字字符串</returns>
public static string ToGB2312(string str)
{
    string tempStr = str;
    MatchCollection mc = Regex.Matches(str, @"\\u([\w]{2})([\w]{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
    byte[] bts = new byte[2];
    foreach (Match m in mc)
    {
        bts[0] = (byte)int.Parse(m.Groups[2].Value, NumberStyles.HexNumber);
        bts[1] = (byte)int.Parse(m.Groups[1].Value, NumberStyles.HexNumber);
        tempStr = tempStr.Replace(m.Value, Encoding.Unicode.GetString(bts));
    }
    return tempStr;
}

 

posted @ 2014-12-12 14:20  冰封的心  阅读(907)  评论(0编辑  收藏  举报