user34537
user34537

Reputation:

How do i escape html unicode in .net?

In a webpage i got the text \u30ca\u30bf\u30ea\u30a2. It should translate to ナタリア. I just dont know how to do it in .NET. I tried HttpUtility.HtmlDecode and when that failed i tried HttpUtility.UrlDecode. No luck

Upvotes: 6

Views: 2268

Answers (1)

Shekhar_Pro
Shekhar_Pro

Reputation: 18430

Ok if your string is Escaped you will need to convert the string manually in to unicode.. or i have a better way. JSON accepts the Escaped Unicode chars and Convert it to normal chars so try this (The JavaScriptSerializer is in System.Web.Script.Serialization in System.Web.Extensions.dll):

string d = @"\u30ca\u30bf\u30ea\u30a2";
Console.WriteLine("Unicode Escaped:" + d);
JavaScriptSerializer jr = new JavaScriptSerializer();
string dt = jr.Deserialize<string>("\"" + d + "\"");
Console.WriteLine("Converted:" + dt);

and the output is :

Unicode Escaped: \u30ca\u30bf\u30ea\u30a2

Converted: ナタリア


And if you still want to do it manually n code . this answer with code on SO is what you want:

Convert a Unicode string to an escaped ASCII string

I don't want too take credit of posting his code.

Upvotes: 2

Related Questions