码迷,mamicode.com
首页 > 其他好文 > 详细

UnicodeToGB2312

时间:2014-12-12 14:42:28      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   sp   for   on   

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;
}

 

UnicodeToGB2312

标签:style   blog   http   io   ar   color   sp   for   on   

原文地址:http://www.cnblogs.com/yibinboy/p/4159649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!