简体   繁体   中英

Cannot convert from UTF8 to ASCII

I used some online encoding detection so all of them are saying that

2015%2d03%2d31

is encoded by UTF8.

So I used this code to decode it and to see

2015-03-31

But it doesn't work.

private static string UTF8toASCII(string text)
        {
            System.Text.Encoding utf8 = System.Text.Encoding.UTF8;
            Byte[] encodedBytes = utf8.GetBytes(text);
            Byte[] convertedBytes =
                    Encoding.Convert(Encoding.UTF8, Encoding.ASCII, encodedBytes);
            System.Text.Encoding ascii = System.Text.Encoding.ASCII;

            return ascii.GetString(convertedBytes);
        }

Any clue?

That's not UTF-8 encoded, it's URL Encoded. Use UrlDecode: https://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode%28v=vs.110%29.aspx

return System.Web.HttpUtility.UrlDecode(text);

If your text might actually contain UTF-8 data, you could use this version:

return System.Web.HttpUtility.UrlDecode(Encoding.UTF8.GetBytes(text), Encoding.UTF8)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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