JavaScript中的二进制转字符串

5
我已经创建了一个JavaScript程序,将字符串转换为二进制。
Input: StackOverflow
Output: 1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111 

现在我想将该二进制转换回像下面这样的字符串。是否有任何可能的方法可以做到这一点?
Input: 1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111 

Output StackOverflow

谢谢


1
你可以在这里查看解决方案:使用JavaScript将二进制转换为文本 - Shivani Sonagara
https://dev59.com/GGEi5IYBdhLWcg3wWLTc#21354328 - ellipsis
3个回答

26

使用String.fromCharCode()parseInt( , 2),如下所示:

const binary = `1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111`;

const outputStr = binary
  // split string into an array of base 2 (binary) UTF-16 chars
  .split(' ')
  // map every binary char to a UTF-16 number of base 10
  // and then get the string representation of that UTF-16 number
  .map(bin => String.fromCharCode(parseInt(bin, 2)))
  // join the array back to a single string
  .join('');

console.log(outputStr);

  • String.fromCharCode(number)将返回一个UTF-16字符编码的字符串
  • parseInt(binary , 2)可以将二进制数转化为十进制数

编辑:

由于String.fromCharCode()函数接受多个字符作为参数,因此您也可以使用扩展运算符(...,如下所示:

const binary = `1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111`;

const outputStr = String.fromCharCode(
  ...binary.split(' ').map(bin => parseInt(bin, 2))
)

console.log(outputStr);

编辑2:

随着时间的推移,由于这个答案获得了更多的访问量,我也将添加另一种方式来执行操作...以防万一:

const str = `StackOverflow`;

const outputStr = str.split('') // split in single chars
  .map(c => c.charCodeAt(0) // get the UTF-16 code (10 base)
             .toString(2)) // transform it back to a string (2 base)
  .join(' ') // make single string from array

console.log(outputStr);


好的兄弟,我明白了一切,但第一个例子中使用的代码“bin”是什么意思? - Subha Jeet Sikdar
@SubhaJeetSikdar bin是传递给map函数的参数名称。map函数接受一个带有一个参数的箭头函数,您可以随意命名。bin也可以被称为someVariable。变量本身表示数组中的一个元素。map函数遍历数组中的所有元素,并在每个元素上应用箭头函数(varName => parseInt(varName, 2))。 - MauriceNino
兄弟,符号 ` 是什么意思? - Subha Jeet Sikdar
这些被称为模板文字。看看它们,在JavaScript中它们非常有用。@SubhaJeetSikdar - MauriceNino
1
注意:如果您通过此方法输入Unicode字符,则会看到长度最长为16位的二进制数字。 - Alnitak

2

str ="01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"
    
    
function binaryAgent(str) {
      return str.split(' ').map(letter=>String.fromCharCode(parseInt(letter, 2))).join('')
     
}
    
console.log(binaryAgent(str))

首先,我们使用 .split() 方法将字符串转换为数组,使用空格字符作为分隔符。
然后,我们使用 .map() 高阶循环方法来遍历每个二进制代码字母。
对于每个二进制代码字母,我们使用 String.fromCharCode() 和 parseInt('each letter',2)。其中,parseInt 方法的第二个参数 "2"(称为基数)用于定义我们要使用的数字系统。
最后,我们可以使用 join('') 方法将处理过的数组重新拼接在一起,但这次不使用空格。

2
请为您的答案添加一些解释,以便观众了解代码的工作原理。 - Not A Bot
1
返回翻译后的文本: 并且解释一下它如何改进已经被接受的答案(提示:它没有,因为除了变量名之外,该代码与已接受的答案完全相同,只是将其封装成一个函数)。 - Alnitak

1

const binary = `1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111`;

const inputStr = binary.split(' ');
const output = [];
inputStr.forEach(item => {output.push(String.fromCharCode(parseInt(item, 2)))});
console.log(output.join(''));


1
使用forEach()push代替.map被评为负分。你只是复制了被接受的答案,结果却更糟糕了。 - Alnitak
@Alnitak 只是另一种选择。不过使用 map 更有意义。 - Godsonaddy
3
使用 foreach/push 而不是 map 是一种反模式。没有好的理由去这么做,而且在任何代码审查中都会被标记为低级错误。 - Alnitak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接