将Unicode字符串转换为普通文字

     Json传输中文时为了防止乱码,通常我们会进行Unicode编码 ,如{userID:"001",nickname:"\u65e0\u8bed\u68a6" }
    下面的代码,将能完成Unicode的与普通字符的转换功能,函数是在网上找的,做个记号

 

 

private string U2CnCode(string str)
{
    Match m;
    Regex r 
= new Regex("(?<code>\\\\u[a-z0-9]{4})", RegexOptions.IgnoreCase);
    
for (m = r.Match(str); m.Success; m = m.NextMatch())
    {
        
string strValue = m.Result("${code}");   //代码
        int CharNum = Int32.Parse(strValue.Substring(24), System.Globalization.NumberStyles.HexNumber);
        
string ch = string.Format("{0}", (char)CharNum);
        str 
= str.Replace(strValue, ch);
    }
    
return str;
}

 

posted on 2009-05-26 15:51  Pharaoh  阅读(475)  评论(0编辑  收藏  举报

导航