简体   繁体   English

十六进制到javascript中的字符串

[英]hex to string in javascript

How can I convert: from: ' \\\\x3c ' to: ' < '; 我怎样才能将:从' \\\\x3c '转换为:' < ';

I tried: 我试过了:

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

does not work. 不起作用。 How I do this? 我是怎么做到的 Thanks in advance! 提前致谢!

使用String.fromCharCode而不是eval ,使用parseInt使用base 16:

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

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;
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM