It's all 1's and 0's. But that's not what you want.

You want to take that String populated with nothing but binary numbers and transform it into something that your users can read. Preferably in their native languages.

So how do you do that?

I'll show you how right here.

Splitting (Without Infinitives)

Let's say you've got this big long String object:

String input = "01100011 01100001 01110010 01100101 01111001 01100100 01100101 01110110 01100101 01101100 01101111 01110000 01101101 01100101 01101110 01110100";

And you want to translate that into standard English. You can do that with this code:

String[] parts = input.split(" ");
StringBuilder sb = new StringBuilder();

for (String part : parts) {
    int val = Integer.parseInt(part, 2);
    String c = Character.toString(val);
    sb.append(c);
}

System.err.println(sb.toString());

Note that the first line there splits the string into blocks of 8-bit Strings. Each String represents a separate letter in the final output.

Now the code can get busy with the translation.

Lightning Loop

After instantiating a new StringBuilder object that will hold the finished output, the code loops through each 8-bit block in the String array.

The first line in the loop translates the 8-bit chunk into a two-digit number. That number represents the numerical code of the character.

Here, "the numerical code of the character" is the UTF-8 value in decimal. That's because Java uses UTF-8 by default and nothing in the code above tells it to use anything else.

But remember: that number is in decimal. That might confuse you.

For example, the first block of 8 bits represents the lowercase letter "c". That Integer.parseInt() method will translate that to 99 because that's the decimal representation of the lowercase letter "c" in UTF-8.

But if you look at UTF-8 charts online, you might notice that the value of lowercase "c" is 63.

So is something wrong?

No. Nothing is wrong.

That's 63 in hexadecimal. Which happens to be 99 in decimal.

In other words, everything is working.

Your Radix Is Overheating

Back to Integer.parseInt(). You might notice that the static method there uses two parameters.

The first parameter is the String to get parsed. That's easy enough.

The second parameter is the radix. That's not easy enough.

What the heck is a radix?

Well you can think of it as the "base." In other words Base 10 (decimal) or Base 16 (hexadecimal) or Base 2 (binary).

And after reading that last sentence you probably know why the second parameter is 2 in the code above. It's telling the JVM to translate that string from Base 2, or binary, representation.

In Base 2, everything is either a 0 or a 1. That's exactly what you see in the input string.

Strength of Character

Once the code has the integer value of the character, it needs to convert that into an actual character in the Latin alphabet. The toString() static method on the Character class was made for such a requirement.

That's going to produce a single character that gets appended to the StringBuilder object.

And that's the end of the loop.

Rinse and repeat. The loop will follow that same practice for each and every block of 8-digit numbers in the input string.

Once the loop completes, the code prints out the resulting string in pretty red letters.

If you run the code above, you'll see this:

careydevelopment

And that's right because that's exactly what that binary sequence represents. It's the String I pulled in from the tutorial on how to convert a String to a binary.

Wrapping It Up

Now you know how to take that complex string of binary characters and convert it into something useful.

Feel free to experiment with your own sequences. 

Have fun!

Photo by Andrea Piacquadio from Pexels