简体   繁体   English

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

[英]How to convert string contains hex chars into string

I get a code from google to say for example. 例如,我从谷歌获得了一个代码。 The bellow example contains non-ascii chars in a string. 下面的示例在字符串中包含非ASCII字符。 What i want is to write a function that can convert them all into ASCII string. 我想要编写一个可以将它们全部转换为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}]"}/*""*/

Here is the function i have but it doesn't work out with the string above: 这是我拥有的功能,但无法与上面的字符串一起使用:

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

thanks for reply. 谢谢您的回复。

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());

您也可以使用以下方法

HttpUtility.UrlDecode(String) from System.Web
    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", "#");
    .......
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM