JavaScript 中的二进制到原始字符串转换
javascriptweb developmentfront end technologyobject oriented programming更新于 2024/1/8 3:24:17
我们需要编写一个 JavaScript 函数,该函数接受一个表示二进制代码的字符串。该函数应返回字符串的字母表示形式。
例如 −
如果二进制输入字符串是 −
const str = '1001000 1100101 1101100 1101100 1101111 100000 1010111 1101111 1110010 1101100 1100100';
那么输出应该是 −
const output = 'Hello World';
示例
其代码为 −
const str = '1001000 1100101 1101100 1101100 1101111 100000 1010111
1101111 1110010 1101100 1100100';
const binaryToString = (binary = '') => {
let strArr = binary.split(' ');
const str = strArr.map(part => {
return String.fromCharCode(parseInt(part, 2));
}).join('');
return str;
};
console.log(binaryToString(str));
输出
控制台中的输出将是 −
Hello World
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

