hex to string in javascript
How can I convert:
from:
'\\x3c
'
to:
'<
';
I tried:
s=eval(s.replace("\\\\", ""));
does not work. How I do this? Thanks i开发者_开发技巧n advance!
Use String.fromCharCode
instead of eval
, and parseInt
using base 16:
s=String.fromCharCode(parseInt(s.substr(2), 16));
If you're 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;
}
One way to do it, which will incur the wrath of people who believe that "eval
" is unequivocally evil, is as follows:
var s = "\\x3c";
var s2 = eval('"' + s + '"'); // => "<"
精彩评论