Why doesn't HtmlEncode encode this char?

This is the code:

Response.Write("asd1 X : " + HttpUtility.HtmlEncode("×")); Response.Write("asd2 X : " + HttpUtility.HtmlEncode("✖")); 

The first one is:

 asd1 X : × // OK, ENCODED AS HTML ENTITIES 

second no, just ✖:

 asd2 X : ✖ 

what kind of char? Also, if I try here , the result will be as follows:

 asd1 X : × asd2 X : ✖ 

What?? Why are these differences?

+4
source share
2 answers

My best guest is that not all lines have an entity representation. X heavy multiplication is one of many that do not.

To clarify the Oded link, HttpUtility.HtmlEncode encodes characters only in ISO 8859-1 (Latin-1) . Since Heavy Multiplication X is outside this range, the function does not process it.

If you try Microsoft.Security.Application.AntiXss.HtmlEncode("✖"); , you get the HTML object in ✖ .

+2
source

On the MSDN page for HttpUtility.HtmlEncode(string) you will find this comment:

It encodes all character codes from a decimal number from 160 to 255 (both inclusive) to their numeric object (for example,   )

× ( × ) is the same as × / × on my computer, so it will be encoded, but since is ✖ / ✖ , it will not happen.

You can use the HtmlEncode overload , which accepts a TextWriter based on the required encoding.

+7
source

All Articles