September 06, 2007

How to Convert Unicode Character to HTML string?

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


//language c#
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;
}

Labels: , , , ,

2 Comments:

Blogger ezsh_ua said...

It's not work on
XMLElement.SetAttribute("Attr", GetUnicode(str));
where value of str in russian language...Because in xml : Attr="&#1054", but not Attr="&#1054"...
How simple replace "&" by "&" in xml document ?

July 29, 2008 at 1:47 PM  
Blogger Palak Chokshi said...

Thanks a lot. This really helped me out.

September 12, 2008 at 12:20 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home