DEV Community

Cover image for Public Solving: Decoding a secret message
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Public Solving: Decoding a secret message

Santa got a super weird email, and at first, he thought he might have been hacked.

But it was just a cool hacker kid not wanting the public to see his letter to Santa.

But Santa doesn't know much about computers and asked us to decode the message he received.

You can find the complete puzzle here.

Thinking about the solution

Let's first look at what we get. There seems to be a message that looks kind of like this:

01001010
01101001
01101110
01100111
01101100
01100101
00100000
01100010
01100101
01101100
01101100
01110011
Enter fullscreen mode Exit fullscreen mode

If you've been through any basic computer science class you might have spotted this is binary code.

Something your computer uses underwater because it only knows ones and zeros.

Knowing this, we can see each line is actually a specific symbol. This could be a letter, symbol, number, or space.

Let's get right into solving this problem so we can feel like Ackerman.

Hackerman gif

Decoding a binary message in JavaScript

The first thing we want to do is make sure we can access all the individual lines.

Knowing they are all on different lines, we can use the split method to split on a new line like so.

input.split('\n')
Enter fullscreen mode Exit fullscreen mode

This will give us an array of binary codes.

And seeing it's now an array, we can use the all-around excellent reduce method.

return input.split('\n').reduce((string, binary) => {
    // todo
}, '');
Enter fullscreen mode Exit fullscreen mode

The reduce takes two arguments: the accumulator (string) and the current looped element (binary).
We set the accumulator default value at the end, and I set it as an empty string.

We need to return the string and append the decoded symbol for this binary code inside.

To decode a binary code, we can use the following JavaScript function.

String.fromCharCode(parseInt(binary, 2))
Enter fullscreen mode Exit fullscreen mode

Two things are happening there:

  1. parseInt: This piece will convert the binary code to a character code.
  2. String.fromCharCode converts the character code to a string.

Let's take the following binary code and see what happens:

const binary = '01001010'
const charCode = parseInt(binary, 2)
// 74
const symbol = String.fromCharCode(charCode)
// J
Enter fullscreen mode Exit fullscreen mode

Meaning that this binary range is the letter J.

Now let's use this and combine it into the reduce function.

return input.split('\n').reduce((string, binary) => {
    return (string += String.fromCharCode(parseInt(binary, 2)));
}, '');
Enter fullscreen mode Exit fullscreen mode

And that's it!
We now have a binary decoder in JavaScript ๐Ÿ˜Ž.

Look at us being hackers.

There is only one more thing to do,
Run the tests.

All test succeeded

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (4)

Collapse
 
lexlohr profile image
Alex Lohr • Edited

I'd directly use input.replace(/([01]+)\n?/g, (_, c) => String.fromCharCode(parseInt(c, 2))) instead of resorting to arrays. Look, Ma, imma Hackerman!

Collapse
 
dailydevtips1 profile image
Chris Bongers

Fair enough ๐Ÿ˜‚

Thought the reduce was quite readable.
But indeed shorter lifecycle solution. ๐Ÿ‘

Did you participate in these challenges as well Alex?

Collapse
 
lexlohr profile image
Alex Lohr

No, I don't have the time. It's faster and easier to improve existing solutions ;-)

Thread Thread
 
dailydevtips1 profile image
Chris Bongers

Always love your feedback though!
Never done learning and improving ๐Ÿ‘