So you've got a String that you'd like to convert to binary in your Java application? No problem, I'll show you how to do it here.

Just keep in mind: it's a multi-step process.

Sure, you could get away with doing it in just a couple of lines. That would look cool to people who graduated from MIT.

But you probably want your code a little more readable. I'll show you how to do it the readable way.

Split It Up

The first thing you need to do is split that String into a sequence of characters. Then translate each one of those characters into an 8-bit binary. Then string all those binaries together for your final output.

Here's what the code looks like:

String input = "careydevelopment";
StringBuilder sb = new StringBuilder();

char[] chars = input.toCharArray();
for (char c : chars) {
    String binary = Integer.toBinaryString(c);
    String formatted = String.format("%8s", binary);
    String output = formatted.replaceAll(" ", "0");
    sb.append(" ");


The first line instantiates the input String. It's just the word "careydevelopment".

The second line creates a StringBuilder object. That's what the code will use to create the final output.

Now on to the third line. That's where you see the code converting the input String to an array of characters. It does that with the aid of the toCharArray() method that's part of the String class.

Lovin' the Loop

Once the code has that array of characters, it iterates through each character to convert it to binary.

That binary conversion happens in the very first line inside the loop. The code uses the static method toBinaryString() that's part of Integer.

Yep. You have to use the Integer class if you want binary output.

I don't make the rules. I just report.

Anyhoo, that output will give you a binary String. But it won't necessarily be 8-bits long.

You see, the input character might just require three bits if its numeric representation is on the low side.

Well sometimes that's all you need, just 3 or 4 bits instead of a full 8-bit package.

But most of the time, you'll want the output to look purty.

Looking for 8

George Strait once sang, "I'll be looking for 8 when they pull that gate" in Amarillo by Morning. But that has nothing to do with this guide.

What does have something to do with this guide is the fact that you always want your binary representations to appear in blocks of 8. There are 8 bits to a byte, after all.

So if Integer.toBinaryString() returns a 3-character value (or 3-bit value), you want to pad leading 0's in front of those three characters so that you make it 8 characters without changing the value.

And that's why these two lines exist in the code above:

    String formatted = String.format("%8s", binary);
    String output = formatted.replaceAll(" ", "0");

The first line formats the output string to 8 characters. The format string "%8s" means "make the string 8 characters long no matter what."

But it pads the string with leading spaces, not 0's. So "010" becomes "     010".

The second line takes care of that by replacing the spaces with 0's. And "010" becomes "00000010".


Here are the last two lines in the loop:

    sb.append(" ");

The first line appends the newly created binary string to the StringBuilder object. 

The second line adds a space so each block of 8 characters stands apart.

If you run that code, you'll see this output:

01100011 01100001 01110010 01100101 01111001 01100100 01100101 01110110 01100101 01101100 01101111 01110000 01101101 01100101 01101110 01110100 

And that's the binary representation of "careydevelopment" so the experiment is a success.

Wrapping It Up

Congratulations! You've converted a String to a binary using Java code.

Now try it out with different strings.

Experiment. Play around. Learn more.

Have fun!

Photo by Mikhail Nilov from Pexels