Where is the error? I want to convert a binary number to ASCII-text. The result is not so good. Or is this code correct?
For example:
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
The result is:
®Òä@Ä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ÐØ@
Code:
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;
}
Where is the error? I want to convert a binary number to ASCII-text. The result is not so good. Or is this code correct?
For example:
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
The result is:
®Òä@Ä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ÐØ@
Code:
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;
}
Share
Improve this question
edited Apr 13, 2015 at 8:02
Oleg
9,3692 gold badges45 silver badges59 bronze badges
asked Nov 11, 2011 at 19:40
SmoothieSmoothie
2614 silver badges11 bronze badges
5
-
1
Where is
bits
defined? – Platinum Azure Commented Nov 11, 2011 at 19:42 - the correct result should be a text without umlauts – Smoothie Commented Nov 11, 2011 at 19:45
- The result should be: 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 Umlauts are correctly replaced, but i'm wondering about the encoding. Und ich will die Wörter nicht zählen :D – NCode Commented Nov 11, 2011 at 19:46
- Try to break down the problem a bit - translate one at a time and see what you get, test it. Think about what you expect to see. – Paul Butcher Commented Nov 11, 2011 at 19:46
- a "bit" letters are correct for example "aehle" in my result. – Smoothie Commented Nov 11, 2011 at 19:49
3 Answers
Reset to default 4function 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));
Result:
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
JSFiddle
Alternative:
var res = str.match(/[01]{8}/g).map(function(v) {
return String.fromCharCode( parseInt(v,2) );
}).join('');
ht tp://jsfiddle/Wy93E/1/
Alternative 2:
var res = str.replace(/[01]{8}/g, function(v) {
return String.fromCharCode( parseInt(v,2) );
});
ht tp://jsfiddle/Wy93E/2/
Alternative 3:
var res = String.fromCharCode.apply(null, str.match(/[01]{8}/g).map(function(v) {
return parseInt(v,2)
}));
ht tp://jsfiddle/Wy93E/4/
If you just want to convert binary string to ASCII letters, this kind of simple function does the trick.
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;
}
The problem is the spaces in the string:
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"
Disclaimer: string bracket notation doesn't work in older browsers