本文概述
二进制代码是使用两个符号系统来表示文本, 计算机处理器指令或其他数据的表示形式, 通常二进制数字系统为0和1。二进制代码为每个字符, 指令分配一种二进制数字(位)的模式例如, 八位二进制字符串可以表示256个可能的值中的任何一个, 因此可以表示各种不同的项目。尽管对于其他语言而言, 使用Python或C ++(使用bitset <8>)之类的语言将字符串转换为二进制表示符并不是那么容易和快捷, 但仍可以使用3或4行代码来实现。
在本文中, 你将学习如何使用Javascript将字符串转换为二进制值, 以及将二进制值转换为人类可读的字符串。
测验
在本文中, 我们将与你分享一些将可读字符串转换为二进制表示形式或将二进制字符串转换为可读字符串的方法。为了检查这些方法是否按预期工作, 我们将在以下变量TestBlock中存储的每个对象上测试该方法:
var TestBlock = [
{
binary: "01010010 01100101 01100001 01101100 00100000 01110000 01110010 01101111 01100111 01110010 01100001 01101101 01110011 00100000 01100100 01101111 01101110 00100111 01110100 00100000 01100101 01100001 01110100 00100000 01100011 01100001 01100011 01101000 01100101 00101110", text: "Real programs don't eat cache."
}, {
binary: "010011010110111101110011011101 0000100000011100000111010101100010011011000110100101100011001000 000110010001101111011011010110000101101001011011100010000001110011011011110110011001110100011101110110000101110010011001010010000001101001011100110010000001100110011100100110010101100101001011000010000001100001011101000010000001101100011001010 1100001011100110111010000100000011000010111010000100 000011001100110100101110010011100110 11101000010000001100111011011000110000101101110011000110110010100101110", text: "Most public domain software is free, at least at first glance."
}, {
binary: "010101000110100 00110111101110011011001010010000001110111011010000110111100100000011001000110111 1001000000110111 00110111101110100001000000111010101101110011001000110010101110010011100110111010001100001011011100110010000100 00001010101011011100110100101111000001000000110000101110010011001010010000001100011011011110110111001100100011001010110110101101110011001010110 0100001000000111010001101111001000000111001001100101011010010110111001110110011001010110111001 110100 00100000011010010111010000101100001000000111000001101111011011110111001001101100011110010010111000100000001011010010110100100000010010000110010101101110011100100111100 10010000001010011011100000110010101101110011000110110010101110010", text: "Those who do not understand Unix are condemned to reinvent it, poorly. -- Henry Spencer"
}, {
binary: "01000110010011110101001001010100 0101001001000001010011100010000001101001011100110010000001100110011011110111001000100000011100000110100101110000011001010010000001110011011101000111001001100101011100110111001100100000011001100111001001100101011000010110101101110011001000000110000101101110011001000010000001100011011100 1001111001011100110111010001100001011011000110110001101111011001110111001001100001011100000110100001111001001000000111011101100101011001010110111001101001011001010111001100101110", text: "FORTRAN is for pipe stress freaks and crystallography weenies."
}, {
binary: "00101111 01100101 01100001 01110010 01110100 01101000 00100000 01101001 011100 11 00100000 00111001 00111000 00100101 00100000 01100110 01110101 01101100 01101 100 00100000 00101110 00101110 00101110 00100000 01110000 01101100 01100101 01100001 01110011 01100101 00100000 01100100 01100101 01101100 01100101 01110100 01100101 00100000 01100001 0110 1110 011110 01 01101111 01101110 01100101 00100000 01111001 01101111 01110101 00100000 01100011 01100001 01101110 00101110 ", text: "/earth is 98% full ... please delete anyone you can."
}
];
话虽如此, 让我们开始吧!
可读的字符串到二进制
要将人类可读的字符串转换为其二进制值, 请使用以下函数:
/**
* Function that converts a string into its binary representation
*
* @see https://gist.github.com/eyecatchup/6742657
* @author https://github.com/eyecatchup
*/
function stringToBinary(str, spaceSeparatedOctets) {
function zeroPad(num) {
return "00000000".slice(String(num).length) + num;
}
return str.replace(/[\s\S]/g, function(str) {
str = zeroPad(str.charCodeAt().toString(2));
return !1 == spaceSeparatedOctets ? str : str + " "
});
};
你可以轻松使用它:
// "01001000 01100101 01101100 01101100 01101111 00100000 01000010 01101001 01101110 01100001 01110010 01111001 00100000 01010111 01101111 01110010 01101100 01100100"
stringToBinary("Hello Binary World");
但是这段代码如何工作? Providen字符串charCodeAt方法返回字符串中第一个字符的Unicode。 toString()方法解析其第一个参数, 并尝试返回指定基数(基数)的字符串表示形式。对于基数大于10的字母, 字母表示大于9的数字。例如, 对于十六进制数字(基数为16), 使用a至f。
如果将第二个参数(spaceSeparatedOctets)提供为0, 则字符串将不会被”漂亮地打印”(八位字节将不会被分隔)。下面是对stringToBinary方法的测试(请注意, stringToBinary方法在返回的字符串的末尾添加了一个空字符, 因此建议使用trim方法将其删除):
var testsSuccesfullyExecuted = 0;
var testErrors = [];
TestBlock.forEach(function(item , index){
var processedBinaryString = item.binary;
// Removes the spaces from the binary string
processedBinaryString = processedBinaryString.replace(/\s+/g, '');
// Pretty (correct) print binary (add a space every 8 characters)
processedBinaryString = processedBinaryString.match(/.{1, 8}/g).join(" ");
// Remove spaces at the end of the stringToBinary generated string
if(stringToBinary(item.text).trim() == processedBinaryString){
console.log("Test ${"+ index +"} passes");
testsSuccesfullyExecuted++;
}else{
testErrors.push(index);
}
});
if(testsSuccesfullyExecuted == TestBlock.length){
console.log("Test suite succesfully executed with no errors");
}else{
console.error("Test failed with : " + JSON.stringify(testErrors));
}
在控制台中提供以下输出:
Test ${0} passes
Test ${1} passes
Test ${2} passes
Test ${3} passes
Test ${4} passes
Test suite succesfully executed with no errors
在具有以下规格的个人计算机中执行的标准基准测试:
- Windows 10 Pro 64位(10.0, 内部版本14393)-Chrome版本56.0.2924.87(64位)
- Intel(R)CoreTM i7-7700 CPU @ 3.60GHz(8 CPU), 〜3.6GHz
- 8192MB内存
函数stringToBinary需要1322.53毫秒才能执行10K次测试, 每个任务的平均执行时间为0.13194000000025843毫秒。
二进制到可读字符串
要将二进制字符串转换为可读字符串, 可以使用以下两种方法中的任何一种binaryToString或binaryAgent:
选项1
此函数删除字符串的所有空白, 并将其拆分为8个字符的块, 这些块将合并为单个字符串。然后, String.fromCharCode将为你解决问题:
function binaryToString(str) {
// Removes the spaces from the binary string
str = str.replace(/\s+/g, '');
// Pretty (correct) print binary (add a space every 8 characters)
str = str.match(/.{1, 8}/g).join(" ");
var newBinary = str.split(" ");
var binaryCode = [];
for (i = 0; i < newBinary.length; i++) {
binaryCode.push(String.fromCharCode(parseInt(newBinary[i], 2)));
}
return binaryCode.join("");
}
以及用法:
// Both of them return: "Hello Binary World"
binaryToString("0100100001100101011011000110110001101111001000000100001001101001011011100110000101110010011110010010000001010111011011110111001001101100011001000010000000100001");
binaryToString("01001000 01100101 01101100 01101100 01101111 00100000 01000010 01101001 01101110 01100001 01110010 01111001 00100000 01010111 01101111 01110010 01101100 01100100 00100000 00100001");
binaryToString的测试代码如下:
var testsSuccesfullyExecuted = 0;
var testErrors = [];
TestBlock.forEach(function(item , index){
if(binaryToString(item.binary) == item.text){
console.log("Test ${"+ index +"} passes");
testsSuccesfullyExecuted++;
}else{
testErrors.push(index);
}
});
if(testsSuccesfullyExecuted == TestBlock.length){
console.log("Test suite succesfully executed with no errors");
}else{
console.error("Test failed with : " + JSON.stringify(testErrors));
}
以下测试将在控制台中输出以下内容:
Test ${0} passes
Test ${1} passes
Test ${2} passes
Test ${3} passes
Test ${4} passes
Test suite succesfully executed with no errors
选项2
以第一个函数的相同方式, 将删除所有空白区域, 并将字符串分成8个字符的块, 最后进行连接。
function binaryAgent(str) {
// Removes the spaces from the binary string
str = str.replace(/\s+/g, '');
// Pretty (correct) print binary (add a space every 8 characters)
str = str.match(/.{1, 8}/g).join(" ");
return str.split(" ").map(function (elem) {
return String.fromCharCode(parseInt(elem, 2));
}).join("");
}
以及用法:
// Both of them return: "Hello Binary World"
binaryAgent("0100100001100101011011000110110001101111001000000100001001101001011011100110000101110010011110010010000001010111011011110111001001101100011001000010000000100001");
binaryAgent("01001000 01100101 01101100 01101100 01101111 00100000 01000010 01101001 01101110 01100001 01110010 01111001 00100000 01010111 01101111 01110010 01101100 01100100 00100000 00100001");
binaryAgent的测试代码如下:
var testsSuccesfullyExecuted = 0;
var testErrors = [];
TestBlock.forEach(function(item , index){
if(binaryAgent(item.binary) == item.text){
console.log("Test ${"+ index +"} passes");
testsSuccesfullyExecuted++;
}else{
testErrors.push(index);
}
});
if(testsSuccesfullyExecuted == TestBlock.length){
console.log("Test suite succesfully executed with no errors");
}else{
console.error("Test failed with : " + JSON.stringify(testErrors));
}
以下测试将在控制台中输出以下内容:
Test ${0} passes
Test ${1} passes
Test ${2} passes
Test ${3} passes
Test ${4} passes
Test suite succesfully executed with no errors
基准测试
使用以下规格的个人计算机进行标准基准测试(执行10K次)的结果:
- Windows 10 Pro 64位(10.0, 内部版本14393)-Chrome版本56.0.2924.87(64位)
- Intel(R)CoreTM i7-7700 CPU @ 3.60GHz(8 CPU), 〜3.6GHz
- 8192MB内存
已产生以下结果:
方法 | Total time (MS) | 平均时间(MS)/任务 |
binaryToString (Option 1) | 1068.54 | 0.10662899999999208 |
binaryAgent (Option 2) | 1304.80 | 0.13019300000001968 |
如你所见, 第一种方法比第二种方法稍快。
编码愉快!
评论前必须登录!
注册