错误在哪里?我想将二进制数字转换为ASCII文本。结果不是很好。还是此代码正确?
例如:
01010111011010010111001000100000011000100110 01010110011101101001011011100110111001100101 01101110001000000110110101101001011101000010 00000110010101110100011101110110000101110011 00100000011001010110100101101110011001100110 00010110001101101000011001010110110100101110 00100000010101110110010101101110011011100010 00000110010001110101001000000110010001101001 01100101011100110010000001101100011001010111 00110110010101101110001000000110101101100001 01101110011011100111001101110100001011000010 00000110100001100001011100110111010000100000 01100100011101010010000001110011011000110110 10000110111101101110001000000110100001100101 01110010011000010111010101110011011001110110 01010110011001110101011011100110010001100101 01101110001011000010000001100100011000010111 00110111001100100000011001000110100101100101 01110011011001010111001000100000010101000110 01010111100001110100011000100110110001101111 01100011011010110010000001100101011010010110 11100110010100100000011000100110100101101110 01100001011001010111001001100101001000000100 01000110000101110010011100110111010001100101 01101100011011000111010101101110011001110010 00000110010001100101011100110010000001010100 01100101011110000111010001100101011100110010 00000110100101110011011101000010110000100000 01100100011001010110111000100000011001000111 01010010000001100111011001010111001001100001 01100100011001010010000001101100011010010110 01010111001101110100001011100010000001000010 01101001011101000111010001100101001000000111 10100110000101100101011010000110110001100101 00100000011001000110100101100101001000000101 01110100111101000101010100100101010001000101 01010010001000000110010001101001011001010111 00110110010101110011001000000100001001101100 01101111011000110110101101110011001011000010 00000111101001101001011001010110100001100101 00100000001100010011001100100000011001000110 00010111011001101111011011100010000001100001 01100010001000000111010101101110011001000010 00000110111001101111011101000110100101100101 01110010011001010010000001100100011000010111 00110010000001000101011100100110011101100101 01100010011011100110100101110011001000000110 00010110110001110011001000000101101001100001 01101000011011000010000001000001
结果是:
®Òä@Äginne76´º]Ø\-Ìf6VÒ»+sqÔÊæ@ØÊen ka779º\ݤl6öâ«;Õ¹ÜX@ÈÂs die9²¹*^mdæR&+)ÉÍÑØØêÜÎdes T2¼:2¹\ÝÄGRvW&#)cKÍиÒèèÊ@aehle24²ÓÑTD,W6W2&{[a饡@bf@Èvon a1:·2ÝYL¤2W&vsK±ÌiÐØ@
码:
function bin(text){
var bins = new Array();
var byte = "";
var chary = "";
for(var i = 0; i<text.length; i++){
byte += text.charAt(i);
if(i%bits==0){
bins.push(byte);
byte = "";
}
}
for(var i = 0; i<bins.length; i++){
var binary = bins[i];
var decimal = 0;
var index = 0;
while(binaerzahl > 0){
rest = binary % 2;
binary = Math.floor(binary / 10);
decimal += rest * Math.pow(2, index);
index++;
}
chary += String.fromCharCode(decimal);
}
return chary;
}
最佳答案
function bin (text) {
var res = '', j;
for (var i = 0 ; i < text.length; i = j) {
j += 8; // specify radix--v
res += String.fromCharCode( parseInt( text.slice( i, j ), 2 ) );
}
return res;
}
// remove spaces from the string
var str = '01010111011010010111001000100000011000100110010101100111011010010110111001101110011001010110111000100000011011010110100101110100001000000110010101110100011101110110000101110011001000000110010101101001011011100110011001100001011000110110100001100101011011010010111000100000010101110110010101101110011011100010000001100100011101010010000001100100011010010110010101110011001000000110110001100101011100110110010101101110001000000110101101100001011011100110111001110011011101000010110000100000011010000110000101110011011101000010000001100100011101010010000001110011011000110110100001101111011011100010000001101000011001010111001001100001011101010111001101100111011001010110011001110101011011100110010001100101011011100010110000100000011001000110000101110011011100110010000001100100011010010110010101110011011001010111001000100000010101000110010101111000011101000110001001101100011011110110001101101011001000000110010101101001011011100110010100100000011000100110100101101110011000010110010101110010011001010010000001000100011000010111001001110011011101000110010101101100011011000111010101101110011001110010000001100100011001010111001100100000010101000110010101111000011101000110010101110011001000000110100101110011011101000010110000100000011001000110010101101110001000000110010001110101001000000110011101100101011100100110000101100100011001010010000001101100011010010110010101110011011101000010111000100000010000100110100101110100011101000110010100100000011110100110000101100101011010000110110001100101001000000110010001101001011001010010000001010111010011110100010101010010010101000100010101010010001000000110010001101001011001010111001101100101011100110010000001000010011011000110111101100011011010110111001100101100001000000111101001101001011001010110100001100101001000000011000100110011001000000110010001100001011101100110111101101110001000000110000101100010001000000111010101101110011001000010000001101110011011110111010001101001011001010111001001100101001000000110010001100001011100110010000001000101011100100110011101100101011000100110111001101001011100110010000001100001011011000111001100100000010110100110000101101000011011000010000001000001';
console.log(bin(str));
结果:
一切都在进行中。温恩·迪斯·莱森·坎斯特(Wenn du lesen lesen kannst),哈斯特·迪·史雄·赫拉斯·基芬(Hast du schon herausgefunden),达斯·迪瑟尔Textblock eine binaere Darstellung des Textes ist,登·迪·杰拉德·莱斯特。 Bitte zaehle die WOERTER dieses Blocks,ziehe 13 davon ab and notiere das Ergebnis als Zahl A
JSFiddle
选择:
var res = str.match(/[01]{8}/g).map(function(v) {
return String.fromCharCode( parseInt(v,2) );
}).join('');
ht tp://jsfiddle.net/Wy93E/1/
选择2:
var res = str.replace(/[01]{8}/g, function(v) {
return String.fromCharCode( parseInt(v,2) );
});
ht tp://jsfiddle.net/Wy93E/2/
选择3:
var res = String.fromCharCode.apply(null, str.match(/[01]{8}/g).map(function(v) {
return parseInt(v,2)
}));
ht tp://jsfiddle.net/Wy93E/4/
关于javascript - 使用JS将二进制数转换为ASCII文本时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8099078/