Decoding a hex string in javascript

I have a line in JS in this format:

HTTP \ X3A \ x2f \ x2fwww.url.com

How can I get the decoded string from this? I tried unescape (), string.decode, but this does not decrypt it. If I show this encoded string in a browser, it looks fine (http://www.url.com), but I want to process this string before displaying it.

Thanks.

+5
javascript
source share
5 answers

You can write your own replacement method:

String.prototype.decodeEscapeSequence = function() { return this.replace(/\\x([0-9A-Fa-f]{2})/g, function() { return String.fromCharCode(parseInt(arguments[1], 16)); }); }; "http\\x3a\\x2f\\x2fwww.example.com".decodeEscapeSequence() 
+11
source share

There is nothing to decipher. \xNN is a JavaScript escape character that denotes a character with NN code. An escape character is just a way of specifying a string - when it is parsed, it is already "decoded", so it displays the browser perfectly.

When you do:

 var str = 'http\x3a\x2f\x2fwww.url.com'; 

it is internally stored as http://www.url.com . You can directly manipulate this.

+10
source share

You do not need to decrypt it. You can safely manipulate it:

 var str = "http\x3a\x2f\x2fwww.url.com"; โ€‹alert(str.charAt(4)); // : alert("\x3a" === ":"); // true alert(str.slice(0,7))โ€‹; // http:// 
+5
source share

If you already have:

 var encodedString = "http\x3a\x2f\x2fwww.url.com"; 

Then , manually decoding the string is not required . The JavaScript interpreter will already decode the escape sequences for you, and in fact double unescaping can cause your script to not work properly with some lines. If, on the contrary, you have:

 var encodedString = "http\\x3a\\x2f\\x2fwww.url.com"; 

These backslashes are considered escaped (so hexadecimal escape sequences remain unencoded), so keep reading.

The easiest way in this case is to use the eval function, which runs its argument as JavaScript code and returns the result:

 var decodedString = eval('"' + encodedString + '"'); 

This works because \x3a is valid JavaScript escape code. However, do not do this if the line does not come from your server ; if so, you will create a new security vulnerability because eval can be used to execute arbitrary JavaScript code.

A better (but less concise) approach would be to use the JavaScript string replacement method to create valid JSON, and then use the browser JSON parser to decode the resulting string:

 var decodedString = JSON.parse('"' + encodedString.replace(/([^\\]|^)\\x/g, '$1\\u00') + '"'); // or using jQuery var decodedString = $.parseJSON('"' + encodedString.replace(/([^\\]|^)\\x/g, '$1\\u00') + '"'); 
+1
source share

maybe this helps: http://cass-hacks.com/articles/code/js_url_encode_decode/

 function URLDecode (encodedString) { var output = encodedString; var binVal, thisString; var myregexp = /(%[^%]{2})/; while ((match = myregexp.exec(output)) != null && match.length > 1 && match[1] != '') { binVal = parseInt(match[1].substr(1),16); thisString = String.fromCharCode(binVal); output = output.replace(match[1], thisString); } return output; } 
+1
source share

All Articles