Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the HtmlEncode doesn't encode this char?

This is the code :

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

The fist one is :

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

the second no, just ✖ :

asd2 X : ✖

which kind of char is that? Also, if I try here the result is :

asd1 X : ×
asd2 X : ✖

What?? Why this differences?

like image 770
markzzz Avatar asked Jan 17 '23 08:01

markzzz


2 Answers

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

It encodes all character codes from decimal 160 to 255 (both inclusive) to their numerical entity (e.g.  )

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

You can use the overload of HtmlEncode that takes a TextWriter based on the wanted Encoding.

like image 58
Oded Avatar answered Jan 25 '23 22:01

Oded


My best guest is that not all strings has a entity representation. The Heavy multiplication X is just one of the many that don't.

To elaborate Oded's link, HttpUtility.HtmlEncode only encodes characters in ISO 8859-1 (Latin-1). Since the Heavy Multiplication X is out of this range, the function doesn't handle it.

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

like image 40
Ray Cheng Avatar answered Jan 25 '23 22:01

Ray Cheng