Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 20, 2021 04:29 am GMT

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:

010010100110100101101110011001110110110001100101001000000110001001100101011011000110110001110011

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('
')

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('
').reduce((string, binary) => { // todo}, '');

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

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)// 74const symbol = String.fromCharCode(charCode)// J

Meaning that this binary range is the letter J.

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

return input.split('
').reduce((string, binary) => { return (string += String.fromCharCode(parseInt(binary, 2)));}, '');

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


Original Link: https://dev.to/dailydevtips1/public-solving-decoding-a-secret-message-27ig

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To