如何在String.fromCharCode()中使用变量?

时间:2017-11-22 18:39:56

标签: javascript

我正在尝试获取下面的代码,将unicode转换为字符串,但没有运气,请帮帮忙?

function rot13(str) {
  var message = "";
  for (var i = 0; i < str.length; i++) {
    message += str.charCodeAt(i) + " ";
  }
  message = message.split(" ").filter(Boolean).join(",");
  return String.fromCharCode(message);
}
console.log(rot13("Hello World"))

2 个答案:

答案 0 :(得分:2)

fromCharCode("1,1,1,1")与您需要fromCharCode(1,1,1,1)使用数组的function rot13(str) { var message = ""; for (var i = 0; i < str.length; i++) { message += str.charCodeAt(i) + " "; } message = message.split(" ").filter(Boolean); return String.fromCharCode.apply(String, message); } console.log(rot13("Hello World"))不同。

&#13;
&#13;
woocommerce_product_query_tax_query
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你想要这样的东西: http://buildingonmud.blogspot.com/2009/06/convert-string-to-unicode-in-javascript.html

&#13;
&#13;
function toUnicode(theString) {
  var unicodeString = '';

  for (var i = 0; i < theString.length; i++) {
    var theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();

    while (theUnicode.length < 4) {
      theUnicode = '0' + theUnicode;
    }

    theUnicode = '\\u' + theUnicode;
    unicodeString += theUnicode;
  }

  return unicodeString;
}

console.log(toUnicode("Hello World"));
&#13;
&#13;
&#13;