Karthick Karthick - 2 years ago 72
HTML Question

Escape Accents To HtmlEntity as Hex

This is my unicode String


The above String need to be converted to HTML Entity (Hex) as


Below is the code, it converts the string to html entiry but in Decimal.

Can anyone help me to get the desired result?

static string EscapeAccentsToHtmlEntities(string source)
int length = source.Length;
var escaped = new StringBuilder();

for (int i = 0; i < length; i++)
char ch = source[i];

if ((ch >= '\x00a0') && (ch < 'Ā')) //U+{0:X4}
escaped.AppendFormat("&#{0};", ((int)ch).ToString(NumberFormatInfo.InvariantInfo)); //"&#{0};"

return escaped.ToString();

possible duplicates of this is for javascript / jquery

Answer Source

You just need to use the correct ToString() format for the integer:

escaped.AppendFormat("&#x{0};", ((int)ch).ToString("X4"));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download