function binaryAgent(str) {
str = str.split(" ")
// splits so i can get each bit of binary
var values = [1, 2, 4, 8, 16, 32, 64];
// values for binary (powers of 2 as you go up)
var letterBinary = 0;
// this is where i will total the value of the binary bit
var sentence = [];
var check = []
for (var i = 0; i < str.length; i++) {
str[i] = str[i].split("").reverse()
// go through, reverse and grab each bianry bit
for (var j = 0; j < str[i].length; j++) {
// checking each number. I start at 1 since the first digit is ignored
if (Number(str[i][j]) === 1) {
// if the current number is a 1, we will find its value in values
letterBinary += values[Number(str[i][j])];
// adding that value to the sum
}
}
check.push(letterBinary)
sentence.push(String.fromCharCode(letterBinary));
// turning that number to hopefully a letter
letterBinary = 0;
// reseting the value of the binary each bit of binary
}
console.log(check)
return sentence.join('');
// return finallized sentence
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
// input
My code loops through each section of binary and check if its a 1 or a 0. If its a one it adds its corresponding value in the values array. You can go through more in depth if you read the comments.
? I fixed my code too, it was starting from the 1st character instead of 0nd. I put in a check array to see all my letterBinary’s. Plus letterBinary is a number in my code, not a letter.
I think there’s a problem with my code. 00111111 which is turned into 11111100, outputs 12. That seems low. I think it should return 63 because 1+2+4+8+16+32, which is a question mark in Aren't bonfires fun!?.
I use letterBinary += values[Number(str[i][j])]; to access the array of values. I did
the current Binary clip (i) → How far i was into the binary clip (j).
Why am i getting 12 instead of 63? Why aren’t i converting correctly? (still confused on your explanation)
You want the position where you find a “2”. Right now you’re just adding the value “2” (after converting it to a number) to your letterBinary value instead of the values from the values array.
@camperextraordinaire - I knew it was one, instead of finding its relative position in the values, i was going to the 2nd item. I was just saying values[1] --> (2) instead of j which was how far i was into my binary clip. That is why 6 was 12. Thanks any ways for your help, you did catch a error in my code in your first reply
Yeah, i watched a tutorial on how to convert Binary to English where they did exactly this but by hand and looked the number on the ASC|| chart/ key. New code btw:
function binaryAgent(str) {
str = str.split(" ")
var values = [1, 2, 4, 8, 16, 32, 64];
var letterBinary = 0;
var sentence = [];
var check = []
for (var i = 0; i < str.length; i++) {
str[i] = str[i].split("").reverse()
for (var j = 0; j < str[i].length; j++) {
if (Number(str[i][j]) === 1) {
letterBinary += values[j];
}
}
check.push(letterBinary)
sentence.push(String.fromCharCode(letterBinary));
// turning that number to hopefully a letter
letterBinary = 0;
}
console.log(check)
return sentence.join('');
}
binaryAgent("00111111");