【问题标题】:How to convert string of ASCII codes to string of characters?如何将ASCII码字符串转换为字符串?
【发布时间】:2019-07-31 17:16:07
【问题描述】:

需要将一串ASCII码转换成对应的字符串。 String.fromCharCode() 仅在我直接输入 ASCII 代码时才有效,但我需要为此使用变量名。

code = codeArr.join('.'); // 104,101,108,108,111,32,119,111,114,108,100

String.fromCharCode(104,101,108,108,111,32,119,111,114,108,100); //你好世界

String.fromCharCode(code); // 这返回空白?没有错误只是空白

【问题讨论】:

  • code 是用点连接的一串数字。当String.fromCharCode() 需要1..n 数字时,它应该如何处理这个字符串?
  • 错字。我的意思是 code = codeArr.join(',');
  • 不会让它变得更好;) String.fromCharCode() 期望数字不是字符串。
  • fromCharCode 使用 UTF-16 代码单元。如果您知道您有 ASCII 代码单元或 ISO 8859-1 代码单元,那么说明您为什么可以使用 fromCharCode 的代码注释是有序的。否则,如果您有来自其他字符编码的代码单元,则不能使用 fromCharCode。

标签: javascript string ascii


【解决方案1】:

使用Spread syntax

let code = [104,101,108,108,111,32,119,111,114,108,100];

console.log(String.fromCharCode(...code));

【讨论】:

  • 这很奏效,因为我不必将代码数组转换为字符串,因此我节省了一步。感谢您的回答和链接!
  • @JeffS “因为我不必将代码数组转换为字符串” - 这首先是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
相关资源
最近更新 更多