How to Convert Unicode Character to HTML string

Use function provided below to get HTML code for a Unicode String. Its quite useful when you want to write characters outside ASCII limit.

public static string GetUnicode(string unicodeText) 
{ 
int unicodeVal = 0; 
string encoded = ""; 
foreach( char c in unicodeText) 
{ 
unicodeVal = Convert.ToInt32(c); 
//Debug.WriteLine(c.ToString & " : " & unicodeVal.ToString) 
if((unicodeVal >= 49) && (unicodeVal <= 122)) 
{ 
//in 'ascii' range x30 to x7a which is 0-9A-Za-z plus some punctuation 
encoded += c; // leave as-is 
} 
else 
{ 
// outside 'ascii' range - encode 
encoded += String.Concat("&#", 
unicodeVal.ToString(System.Globalization.NumberFormatInfo.InvariantInfo),
";"); 
} 
} 

return encoded; 
} 

Ask questions related to this topic via comment box.

Subscribe for our monthly newsletter for updated articles and useful code scripts.

Share It

comments powered by Disqus