8

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#?

gotqn
  • 37,902
  • 44
  • 152
  • 231
vivek a.
  • 89
  • 1
  • 1
  • 3
  • 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 Jun 30 '12 at 14:55

6 Answers6

13

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.

dlev
  • 47,166
  • 5
  • 122
  • 132
4

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.

Community
  • 1
  • 1
andynormancx
  • 12,901
  • 6
  • 35
  • 52
2

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

Microsoft.JScript.GlobalObject.unescape();

Microsoft.JScript.GlobalObject.escape();
nhahtdh
  • 54,546
  • 15
  • 119
  • 154
hs.jalilian
  • 105
  • 1
  • 2
0

edit the web.config the following parameter:

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

Stephane Rolland
  • 37,098
  • 33
  • 115
  • 165
dougstefe
  • 11
  • 2
0

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

Rag
  • 6,161
  • 2
  • 29
  • 38
  • Note: this only applies if the string is a hardcoded literal in the ops source code. Maybe he wants to decode some strings that are input into the program. Then ``HttpUtility.UrlDecode`` is the way to go... – Philip Daubmeier Jun 30 '12 at 15:02
0

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

DrAlligieri
  • 211
  • 5
  • 10
  • 1
    Note: this only applies if the string is a hardcoded literal in the ops source code. Maybe he wants to decode some strings that are input into the program. Then ``HttpUtility.UrlDecode`` is the way to go... – Philip Daubmeier Jun 30 '12 at 15:03