开发者

How do i escape html unicode in .net?

开发者 https://www.devze.com 2023-02-10 19:44 出处:网络
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 an开发者_运维技巧d when that failed i t

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 an开发者_运维技巧d when that failed i tried HttpUtility.UrlDecode. No luck


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消