使用JS将二进制数转换为ASCII文本时出错

时间:2011-11-11 19:40:46

标签: javascript text binary ascii

错误在哪里?我想将二进制数转换为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;

}

3 个答案:

答案 0 :(得分:4)

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));

<强>结果:

  

Wir beginnen mit etwas einfachem。 Wenn du die lesen kannst,hast du schon herausgefunden,dass dieser Textblock eine binaere Darstellung des Textes ist,den du gerade liest。 Bitte zaehle die WOERTER dieses Blocks,ziehe 13 davon ab und notiere das Ergebnis als Zahl A

JSFiddle


替代:

var res = str.match(/[01]{8}/g).map(function(v) {
    return String.fromCharCode( parseInt(v,2) );
}).join('');

http://jsfiddle.net/Wy93E/1/


备选方案2:

var res = str.replace(/[01]{8}/g, function(v) { 
    return String.fromCharCode( parseInt(v,2) ); 
});

http://jsfiddle.net/Wy93E/2/


备选方案3:

var res = String.fromCharCode.apply(null, str.match(/[01]{8}/g).map(function(v) { 
    return parseInt(v,2) 
}));

http://jsfiddle.net/Wy93E/4/

答案 1 :(得分:2)

如果您只想将二进制字符串转换为ASCII字母,这种简单的函数可以解决问题。

function bin (text) {
    var output = ''
    for (var i = 0 ; i < text.length; i+= 8) {
        var c = 0;
        for (var j=0; j < 8 ; j++) {
            c <<= 1;
            c |= parseInt(text[i + j]); 
        }
        output += String.fromCharCode(c);
    }
    return output;
}

答案 2 :(得分:0)

问题是字符串中的空格:

var asd = "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";
var letters = [], txt;
asd = asd.replace( /\s+/g, "" );

    for( var i = 0, l = asd.length; i < l; i += 8 ) {
    txt = "";
        for( var j = 0; j <= 7; ++j ) {
        txt += asd[j+i];
        }
    letters.push( String.fromCharCode( parseInt( txt, 2 ) ) );
    }

letters.join( "" );


//"Wir beginnen mit etwas einfachem. Wenn du dies lesen kannst, hast du schon herausgefunden, dass dieser Textblock eine binaere Darstellung des Textes ist, den du gerade liest. Bitte zaehle die WOERTER dieses Blocks, ziehe 13 davon ab und notiere das Ergebnis als Zahl A"

免责声明:字符串括号表示法在旧版浏览器中不起作用