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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:12:55  来源:igfitidea点击:

hex to string in javascript

javascripthex

提问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.fromCharCodeinstead of eval, and parseIntusing base 16:

使用String.fromCharCode代替eval, 并parseInt使用基数 16:

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

回答by Mrchief

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

如果你使用 jQuery,试试这个: $('<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;
}

回答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 + '"'); // => "<"