Binary Agents Help (Solved)

So i’m trying to convert the Binary Agents Binary to text challenge, here is my code:

Repl → https://repl.it/@John_Nicole/Converting-binary

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.


My thing is, why am i getting:

'\u0002\b\u0006\n\u0006\b\u0002\u0006\n\n\b\u0006\b\u0006\b\u0002\b\b\n\u0002\n'

As my output? I thought that ‘String.fromCharCode’ returned a actual letter or number.

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

Change

letterBinary += values[Number(str)[i][j])];

to

letterBinary += values[j]

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.

1 Like

You can use parseInt to get the values of the binary, like this

parseInt("00111111", 2)

1 Like

@iamknox Thanks, i don’t know how i missed that! :smiley:

@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 :smiley:

1 Like

Sometimes it just takes another pair of eyes.

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

@r1chard5mith , what does the 2 mean?

function binaryAgent(str) {
return str.split(" ").map(x=>String.fromCharCode(parseInt(x,2))).join(’’);
}