日期:2014-05-17  浏览次数:20688 次

一个编码的乱码问题!求解决。
调用接口 返回的地址为

\/apps\/\u6211\u7684\u5b58\u50a8\/1.jpg

正常的应该是 ↓

/apps/我的存储/1.jpg

请问一下 中间的这个\u6211\u7684\u5b58\u50a8 怎么转换成 "我的存储"这几个正常的中文呢?

------解决方案--------------------
string str = @"\/apps\/\u6211\u7684\u5b58\u50a8\/1.jpg";
                str = Regex.Replace(str, @"\\u([\w]{2})([\w]{2})", a => {
                    byte[] bts = new byte[2];
                    bts[0] = (byte)int.Parse(a.Groups[2].Value, NumberStyles.HexNumber);
                    bts[1] = (byte)int.Parse(a.Groups[1].Value, NumberStyles.HexNumber);
                    return Encoding.Unicode.GetString(bts);
                });
                //\\/apps\\/我的存储\\/1.jpg

------解决方案--------------------



    public class UnicodeHelper
    {
        public static string Decode(string str)
        {
            Regex regex = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
            return regex.Replace(str, delegate(Match m)
            {
                char ch = (char)Convert.ToInt32(m.Groups[1].Value, 0x10);
                return ch.ToString();