If you're here, it's probably because you're writing some Java code and you need to know how to convert an Integer to a binary.
And I am here to happily show you how to do that thing.
However, there are some caveats. And I'll explain those along the way.
You're going to start the conversion process with a static method. But you probably won't finish it there.
Here's the unfinished code:
int input = 17; String result = Integer.toBinaryString(input); System.out.println(result);
The first line just sets the input integer. It's a value of 17.
The second line handles the conversion with the assistance of that aforementioned static method. It's called toBinaryString() and it's part of the Integer class.
That makes a whole lot of sense in this situation as you're trying to convert an integer to a binary string.
Anyhoo, if you run that code above you'll get this output:
And that also makes a whole lot of sense because that's the binary representation of 17.
But you can do better.
At this point in your software development career, you probably know enough to know that there are 8 bits to a byte. And yet you only see 5 bits in the output above.
The JVM decided to just spit out the minimal amount necessary to give you an accurate binary representation. So technically that's correct.
But it's also a great idea to format your output so that it's always 8 digits long. Here's how you can do that:
int input = 17; String result = Integer.toBinaryString(input); String formatted = String.format("%8s", result); String padded = formatted.replaceAll(" ", "0"); System.out.println(padded);
The two new lines in the code above handle the formatting.
The String.format() line makes the result 8 characters long. The "%8s" format string handles that task.
But that's going to pad the output with preceding spaces. Not digits.
You want to pad the output with 0's. That way you get the same numeric value but with 8 digits.
That's handled with the replaceAll() line. Each space is substituted with a 0.
Now run that code and you'll get this output:
Ah. Much better.
But there's still room for improvement.
As I mentioned above, there are 8 bits to a byte. So what happens if your input number translates to something that's more than a single byte? Any number above 255 will do that.
No problem. You just need to update the String.format() line. Everything else can stay the same.
Specifically, you need to change the "8" in "%8s" to something else that's integrally divisible by 8. Like "%16s" for a maximum of 2 bytes, "%24s" for a maximum of 3 bytes, and so on.
Or you can let the code itself do the heavy lifting for you.
int input = 511; int bits = 8 * ((input / 256) + 1); String formatString = "%" + bits + "s"; String result = Integer.toBinaryString(input); String formatted = String.format(formatString, result); String padded = formatted.replaceAll(" ", "0"); System.out.println(padded);
The second line of code above returns the number of bits the input consumes in memory. It does that by dividing the input by 256, adding 1 to that result, and multiplying the number by 8.
That will return 8 for input numbers between 0 and 255, 16 for input numbers between 256 and 511, and so on.
Armed with that info, the code constructs a format string in the next line. That's the format string used to produce the final output.
That final output, by the way, looks like this:
Wrapping It Up
Keep in mind: the code above will not work for negative numbers. But it can be updated to do so.
And that's your homework. Get to it now.
Photo by Anastasia Shuraeva from Pexels