Hex to string in javascript

How can I convert: from: \\x3c 'to: < ';

I tried:

 s=eval(s.replace("\\\\", "")); 

does not work. How am i doing this? Thanks in advance!

+4
source share
3 answers

Use String.fromCharCode instead of eval and parseInt using base 16:

 s=String.fromCharCode(parseInt(s.substr(2), 16)); 
+11
source

If you are using jQuery, try this: $('<div>').html('\x3c').text()

Else (taken from here )

 function htmlDecode(input){ var e = document.createElement('div'); e.innerHTML = input; return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue; } 
+1
source

One way to do this, which will entail the wrath of people who believe that “ eval ” is unambiguously evil, is as follows:

 var s = "\\x3c"; var s2 = eval('"' + s + '"'); // => "<" 
0
source

All Articles