如何将包含十六进制字符的字符串转换为字符串

时间:2012-10-21 09:20:21

标签: c# string escaping hex

我从谷歌那里得到一个代码来说。 下面的示例包含字符串中的非ascii字符。 我想要的是编写一个可以将它们全部转换为ASCII字符串的函数。

{e:"vLuDULDKEomjiAfgo4CwBg",c:0,u:"https://www.google.com/complete/search?client\x3dserp\x26hl\x3den\x26sugexp\x3dles%3B\x26gs_nf\x3d3\x26pq\x3dC%23%20decode%20hex%20string%20to%20string\x26cp\x3d11\x26gs_id\x3d3w8\x26xhr\x3dt\x26q\x3dhow%20to%20conv\x26tch\x3d1\x26ech\x3d78\x26psi\x3dDrqDUKC_I86fiAfLrYCgCw.1350810126009.3",d:"[\x22how to conv\x22,[[\x22how to conv\\u003Cb\\u003Eert pdf to word\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert flv to mp3\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert prc to pdf\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert jpg to pdf\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert pdf to epub\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert cr2 to jpeg\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert pdf to excel\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert amr to mp3\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert prc to epub\\u003C\\/b\\u003E\x22,0,[]],[\x22how to conv\\u003Cb\\u003Eert flv to avi\\u003C\\/b\\u003E\x22,0,[]]],{\x22j\x22:\x223w8\x22,\x22q\x22:\x22HhWOfQJyxGUsSNB1855GU4lNjzU\x22}]"}/*""*/

这是我的功能,但它不能解决上面的字符串:

        public static string DecodeHex(string data)
    {
        data = data.Replace("\x22", @"""");
        data = data.Replace("\x23", "#");
        data = data.Replace("\x24", "$");
        data = data.Replace("\x25", "%");
        data = data.Replace("\x26", "&");
        data = data.Replace("\x27", "'");
        data = data.Replace("\x28", "(");
        data = data.Replace("\x29", ")");
        data = data.Replace("\x2a", "*");
        data = data.Replace("\x2b", "+");
        data = data.Replace("\x2c", ",");
        data = data.Replace("\x2d", "-");
        data = data.Replace("\x2e", ".");
        data = data.Replace("\x2f", "/");
        data = data.Replace("\x30", "0");
        data = data.Replace("\x31", "1");
        data = data.Replace("\x32", "2");
        data = data.Replace("\x33", "3");
        data = data.Replace("\x34", "4");
        data = data.Replace("\x35", "5");
        data = data.Replace("\x36", "6");
        data = data.Replace("\x37", "7");
        data = data.Replace("\x38", "8");
        data = data.Replace("\x39", "9");
        data = data.Replace("\x3a", ":");
        data = data.Replace("\x3b", ";");
        data = data.Replace("\x3c", "<");
        data = data.Replace("\x3d", "=");
        data = data.Replace("\x3e", ">");
        data = data.Replace("\x3f", "?");
        return data;
    }

感谢您的回复。

3 个答案:

答案 0 :(得分:5)

string url = "https://www.google.com/complete/search?client\x3dserp\x26hl\x3den\x26sugexp\x3dles%3B\x26gs_nf\x3d3\x26pq\x3dC%23%20decode%20hex%20string%20to%20string\x26cp\x3d11\x26gs_id\x3d3w8\x26xhr\x3dt\x26q\x3dhow%20to%20conv\x26tch\x3d1\x26ech\x3d78\x26psi\x3dDrqDUKC_I86fiAfLrYCgCw.1350810126009.3";
Uri uri = new Uri(url);
Console.WriteLine(uri.ToString());

答案 1 :(得分:3)

您也可以使用以下方法

HttpUtility.UrlDecode(String) from System.Web

答案 2 :(得分:0)

    var json1 = DecodeHex(json);
    var jsonDes = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(json1);
    public static string DecodeHex(string data)
    {
        data = data.Replace("\\x22", @"""");
        data = data.Replace("\\x23", "#");
    .......
    }