Karthick Karthick - 7 months ago 21
HTML Question

Escape Accents To HtmlEntity as Hex

This is my unicode String
>


Désastres


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

Désastres


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};"
}
else
{
escaped.Append(ch);
}
}

return escaped.ToString();
}


Explaination:
possible duplicates of this is for javascript / jquery

Answer

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

escaped.AppendFormat("&#x{0};", ((int)ch).ToString("X4"));