Convert Binary Numbers to String Message

Norberto Santiago
CodeX
Published in
4 min readFeb 20, 2022

Have you ever heard of the two-state representation of electromechanical devices? That would be on-off, open-closed, or go–no go. We can think of others such as working-not working, yes-no, like-unlike. So many things in life that are a two-state matter. It makes me think how much alike are human logic and computer logic. For computers, the binary system is their state of mind.

As you probably know, binary numbers consist of two digits, 0 and 1. The number 2, is represented as 10, number 3 as 11, number 4 as 100, and on and on. In computers, 0s and 1s in binary represent OFF or ON, respectively. This way, numbers are represented physically inside the computing device, permitting calculations.

Tron, 1982. Copyright Disney

Check the following charts on this page as an example. Each column represents the number two raised to an exponent, with that exponent's value increasing by one bit as you move through each of the eight positions of a byte. Remember, each byte consists of 8 bits. As you can see, bits with 0 are not counted because they're "turned off."

The next example is 11111111 in binary, the maximum 8-bit value of 255. Reading right to left we have 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 = 255.

All this has been explained and reminded because we'll convert binary to text. Computers convert text and other data into binary with an assigned ASCII (American Standard Code for Information Interexchange) value.

Let's get to the code.

Solution 1: Brute Force

function binaryToEnglishLetters1(str)

Let's call the function binaryToEnglishLetters1, since we're going to convert from binary to our language, this combination of Latin from the Romans and the runic alphabet of Futhorc that slowly evolved to the Modern English we came to write and convert to binary at this exact moment.

let splitStr = str.split(" "), result = [];

We follow by creating splitStr and splitting with the string from the function's argument split with a space. Let's also add the result variable as an empty array.

for(let i = 0; i < splitStr.length; i++) {   result.push(String.fromCharCode(parseInt(splitStr[i])));}return result.join("");

After creating the variables, let's loop through splitStr and iterate the string, pushing to results from the specified sequence of UTF-16 code units using fromCharCode and parsing the string argument, returning the integer of the specified radix (in our case, the binary base of 2 digits). After that, let's call the result and use join("") to return the converted string and not an array with each letter with a comma.

Are we ready? Let's console log what the computer is trying to say.

Alright, are we ready? Let's go!

䘳죡쥎쥅쳓蚠촬촷쥅蚠촭쥅諶蚠

Oh, wait! Why is it not in our alphabet?

That's because we didn't specify the base of the input numbers. Weird right? Let's go ahead and fix it.

result.push(String.fromCharCode(parseInt(splitStr[i], 2)));

You can even try putting other numbers to see what other characters will appear but let’s stick to the correct one and finally the results with our alphabet.

Kraftwerk loves me.

Awesome! The computer just told us Kraftwerk loves it.

Judging by their awesome song Kraftwerk Loves Computer, I totally agree.

Here's the code:

Solution 2: ES6 Spread Operator

In this solution, let's process each number converted to the corresponding characters using the spread operator and use map() to convert each to decimal. We just did the same as the brute force solution in a few lines of code.

Let's see what the computer is telling us this time.

console.log(binaryAgent2("1000010 1110101 1111001 100000 1101001 1110100 101100 100000 1110101 1110011 1100101 100000 1101001 1110100 101100 100000 1100010 1110010 1100101 1100001 1101011 100000 1101001 1110100 101100 100000 1100110 1101001 1111000 100000 1101001 1110100 101100 100000 1110100 1110010 1100001 1110011 1101000 100000 1101001 1110100 101100 100000 1100011 1101000 1100001 1101110 1100111 1100101 100000 1101001 1110100 101100 100000 1101101 1100001 1101001 1101100 101100 100000 1110101 1110000 1100111 1110010 1100001 1100100 1100101 100000 1101001 1110100"))

Means this in Human English:

Buy it, use it, break it, fix it, trash it, change it, mail, upgrade it

It's singing Daft Punk's Technologic. I guess they're Ok with all that song suggest we do them.

Who wouldn’t be ok with this tune?

So here it is, you can use this algorithm for smaller projects and school assignments. I hope you have fun being creative with it.

Happy Coding!

Summary:

  1. Binary System Overview
  2. Convert from Binary to String - Brute Force Solution
  3. Convert from Binary to String - ES6 Spread Operator Solution

References:

  1. Britannica, The Binary System
  2. Computer Hope, Binary
  3. ASCII Codes Table
  4. The Origin Of The English Alphabet (and all its 26 letters), Racoma, Bernadine. Day Translations Blog August 14th, 2018
  5. MDN Web Docs, String.fromCharCode
  6. MDN Web Docs, parseInt()

--

--

Norberto Santiago
CodeX
Writer for

Norberto Santiago is a bilingual full-stack developer. Currently living in the Twin Cities. https://www.norbertosantiago.com/