9

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
  • 42,737
  • 46
  • 157
  • 243
vivek a.
  • 99
  • 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

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.

dlev
  • 48,024
  • 5
  • 125
  • 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
  • 13,421
  • 6
  • 36
  • 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
  • 55,989
  • 15
  • 126
  • 162
hs.jalilian
  • 103
  • 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
  • 38,876
  • 35
  • 121
  • 169
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,405
  • 2
  • 31
  • 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