DEV Community

Florin Pop
Florin Pop

Posted on

Decode a Binary Message using JavaScript

Today I passed 14k subs on Twitter and as usual I wrote a tweet to thank all the people who support me on this journey.

But this time I did something different. Instead of staying: 14,000, I've put the number in binary: 11011010110000.

And something fun happened...

My good friend Mike replied with a message that was in binary format. πŸ™ˆ

That's something he would obviously do (because he's a hacker πŸ˜†).

So now... I had to decode it.

Here is a Video Tutorial if you are interested to see how I did it:

...

Or for you "lazier" people out there, here is the code:

// storing the message
const mes =
    '01011001 01101111 01110101 00100000 01100001 01110010 01100101 00100000 01110111 01100101 01101100 01100011 01101111 01101101 01100101';

const res = mes
    // split the string into an array of strings (removing the ' ')
    .split(' ')
    // map over the substrings and convert them to numbers
    .map(b => parseInt(b, 2))
    // map over again to convert the numbers to characters
    .map(num => String.fromCharCode(num))
    // join the characters back into a string
    .join('');

// getting the answer
console.log(res);

I hope you enjoyed this little tutorial!

Happy Coding! πŸ˜‡

Top comments (7)

Collapse
 
aleksandrhovhannisyan profile image
Aleksandr Hovhannisyan

For anyone wondering, you can also decode this by hand, albeit more slowly:

01010100 01101000 01100001 01110100 00100111 01110011 00100000 01110011 01101111 00100000 01100011 01101111 01101111 01101100 00100000 01000110 01101100 01101111 01110010 01101001 01101110 00100001

Each group of 8 bits is a byte (hence the intentional spacing to make it more obviousβ€”he could've been more cheeky by grouping them all together). And characters on most modern OSes are represented using bytes (1 char = 1 byte).

01010100 = 4 + 16 + 64 = 84 = T in ASCII.

And so on.

Collapse
 
florinpop17 profile image
Florin Pop

Or that, yes πŸ‘

Collapse
 
barzi92367868 profile image
Barzi

Hi Florin, thank you for your post. I really appreciate the way you wrote your code (I'm one of those lazy guys that didn't watch your video), I find it very clean and straightforward. Keep it up!

Collapse
 
florinpop17 profile image
Florin Pop

Haha πŸ˜†

Thank you! Glad that you liked it!

Collapse
 
vaibhavkhulbe profile image
Vaibhav Khulbe

Woah! Didn't know about that. Also, congratulations on your 14k followers on Twitter, I regularly see your tweets they're awesome!!

Collapse
 
florinpop17 profile image
Florin Pop

Thank you! πŸ˜ƒ

Collapse
 
pflash profile image
Precious adeyinka

Great post florin!