admin管理员组

文章数量:1192919

I am trying to analyse some JavaScript, and one line is

var x = unescape("%u4141%u4141 ......"); 

with lots of characters in form %uxxxx.

I want to rewrite the JavaScript in c# but can't figure out the proper function to decode a string of characters like this. I've tried

HttpUtility.HTMLDecode("%u4141%u4141");

but this did not change these characters at all.

How can I accomplish this in c#?

I am trying to analyse some JavaScript, and one line is

var x = unescape("%u4141%u4141 ......"); 

with lots of characters in form %uxxxx.

I want to rewrite the JavaScript in c# but can't figure out the proper function to decode a string of characters like this. I've tried

HttpUtility.HTMLDecode("%u4141%u4141");

but this did not change these characters at all.

How can I accomplish this in c#?

Share Improve this question edited May 8, 2015 at 6:03 gotqn 43.6k46 gold badges165 silver badges254 bronze badges asked Jul 26, 2011 at 17:09 vivek a.vivek a. 991 gold badge1 silver badge3 bronze badges 1
  • 1 You are mixing up two different escaping/unescaping methods: one is for entities inside a HTML file, the other is for URLs, i.e. addresses. – Philip Daubmeier Commented Jun 30, 2012 at 14:55
Add a comment  | 

6 Answers 6

Reset to default 14

You can use UrlDecode:

string decoded = HttpUtility.UrlDecode("%u4141%u4141");

decoded would then contain "䅁䅁".

As other have pointed out, changing the % to \ would work, but UrlDecode is the preferred method, since that ensures that other escaped symbols are translated correctly as well.

You need HttpUtility.UrlDecode. You shouldn't really be using escape/unescape in most cases nowadays, you should be using things like encodeURI/decodeURI/encodeURIComponent.

When are you supposed to use escape instead of encodeURI / encodeURIComponent?

This question covers the issue of why escape/unescape are a bad idea.

You can call bellow method to achieve the same effect as Javascript escape/unescape method

Microsoft.JScript.GlobalObject.unescape();

Microsoft.JScript.GlobalObject.escape();

Change the % signs to backslashes and you have a C# string literal. C# treats \uxxxx as an escape sequence, with xxxx being 4 digits.

In basic string usage you can initiate string variable in Unicode: var someLine="\u4141"; If it is possible - replace all "%u" with "\u".

edit the web.config the following parameter:

< globalization requestEncoding="iso-8859-15" responseEncoding="utf-8" >responseHeaderEncoding="utf-8" in < system.web >

本文标签: what is the c equivalent to javascript39s unescape()Stack Overflow