2

I've been trying to decode %E9(é).

WebUtility.HtmlDecode("%E9")

doesn't work. It puts a ? sign instead of a é.

trinalbadger587
  • 1,905
  • 1
  • 18
  • 36
  • When I use `WebUtility.HtmlEncode("é")`, I get: `é`, not `%E9`. When I run `HtmlDecode("é");`, I get `é`. So I think `HtmlDecode` is not the method you're looking for. – Adam V Sep 01 '15 at 18:46
  • @user3208848, Try `WebUtility.UrlDecode("%C3%A9");` – Eser Sep 01 '15 at 18:50
  • The correct answer to this question is very dependent on where that url with accents is coming from. According to http://stackoverflow.com/questions/912811/what-is-the-proper-way-to-url-encode-unicode-characters the standard on the web is UTF-8, so trying to decode `"%E9"` (which doesn't represent a valid UTF-8 string) is a bit dodgy. – roeland Sep 02 '15 at 05:46

2 Answers2

6

I've tested this and found that HttpUtility.UrlDecode("%E9") returns question mark that You mentioned. It seems that it You have to manually specify appropriate encoding for this to work correctly with %E9 encoded value.

You can try:

HttpUtility.UrlDecode("%E9", System.Text.UnicodeEncoding.Default);

or

HttpUtility.UrlDecode("%E9", System.Text.UnicodeEncoding.UTF7);

Both should return the character decoded as You expected.

Lukasz M
  • 5,635
  • 2
  • 22
  • 29
3

For Url decode, you should use UrlDecode instead of HtmlDecode. Also, you need to specify the encoding for this to work because the encoded value %E9 is not UTF-8 value, and by default UrlDecode returns string decoded using UTF-8.

Addition to Lukasz's answer, you can also use:

HttpUtility.UrlDecode("%E9", System.Text.Encoding.GetEncoding("iso-8859-1"));
kofsp
  • 31
  • 1
  • 3