javascript javascript中的十六进制到字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7178059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
hex to string in javascript
提问by Hyman
How can I convert:
from:
'\\x3c
'
to:
'<
';
我如何转换:从:“ \\x3c
”到:“ <
”;
I tried:
我试过:
s=eval(s.replace("\\", ""));
does not work. How I do this? Thanks in advance!
不起作用。我怎么做?提前致谢!
回答by Digital Plane
Use String.fromCharCode
instead of eval
, and parseInt
using base 16:
使用String.fromCharCode
代替eval
, 并parseInt
使用基数 16:
s=String.fromCharCode(parseInt(s.substr(2), 16));
回答by Mrchief
回答by maerics
One way to do it, which will incur the wrath of people who believe that "eval
" is unequivocally evil, is as follows:
这样做的一种方法会招致那些相信“ eval
”是绝对邪恶的人的愤怒,如下所示:
var s = "\x3c";
var s2 = eval('"' + s + '"'); // => "<"