tags:

views:

33

answers:

2

I have a list of character that display fine in WebBrowser in the form of encoded characters such as €  ... But when posting these characters onto server to I realized that HttpUtility.HtmlDecode cannot convert them to characters as browser did, they all become space.

text = System.Web.HttpUtility.HtmlDecode("€");

I expect it to return € but it return space instead. The same thing happen for some other characters as well.

Does anyone know how to fix this or any workaround?

A: 

This is commonly result of using literal values and mixing UTF-8 and ASCII. In UTF-8 euro sign is encoded as 3 bytes so there is no ASCII counterpart for it.

Update

Your code is illegal if you are using UTF-8 since it only supports the first 128 characters and the rest are encoded is multiple bytes. You need to use the Unicode syntax:

  // !!! NOT HtmlDecode!!!
  text = System.Web.HttpUtility.UrlDecode("%E2%82%AC");

UPDATE

OK, I have left the code as it was but added the comment that it does not work. It does not work because it is not an encoding which is of concern for HTML - it is not an HTML. This is of concern for the URL and as such you need to use UrlDecode instead.

Aliostad
I've tried your code but it didn't work. Moreover, I need a way to encode the like of € to an meaningful letter. I cannot convert € to %E2%82%AC to decode it
thethanghn
See my update. You need to use UrlDecode.
Aliostad
A: 

ASCII is 7-Bit; there are no characters 128 through 255. The MSDN article you linked is following the long tradition of pretending ASCII is 8-Bit; the article actually shows code page 437.

I'm not sure why you're not simply writing € (compatibility?), but € or € should do, too.

Sören Kuklau
I receive the € from customer and he want to display it as Euro sign
thethanghn