### Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

## 1. Overview

In this tutorial, we’ll look briefly at the different ways of printing an integer in binary format in Java.

First, we’ll take a conceptual look. And then, we’ll learn some built-in Java functions for conversion.

## 2. Using Integer to Binary Conversion

In this section, we’ll write our custom method to convert an integer into a binary format string in Java. Before writing the code, let’s first understand how to convert an integer into a binary format.

To convert an integer n into its binary format, we need to:

1. Store the remainder when number n is divided by 2 and update the number n with the value of the quotient
2. Repeat step 1 until the number n is greater than zero
3. Finally, print the remainders in reverse order

Let’s see an example of converting 7 into its binary format equivalent:

1. First, divide 7 by 2: remainder 1, quotient 3
2. Second, divide 3 by 2: remainder 1, quotient 1
3. Then, divide 1 by 2: remainder 1, quotient 0
4. And finally, print the remainders in reverse order since the quotient in the previous step is 0: 111

Next, let’s implement the above algorithm:

``````public static String convertIntegerToBinary(int n) {
if (n == 0) {
return "0";
}
StringBuilder binaryNumber = new StringBuilder();
while (n > 0) {
int remainder = n % 2;
binaryNumber.append(remainder);
n /= 2;
}
binaryNumber = binaryNumber.reverse();
return binaryNumber.toString();
}``````

## 3. Using Integer#toBinaryString Method

Java’s Integer class has a method named toBinaryString to convert an integer into its binary equivalent string.

Let’s look at the signature of the Integer#toBinaryString method:

``public static String toBinaryString(int i)``

It takes an integer argument and returns a binary string representation of that integer:

``````int n = 7;
String binaryString = Integer.toBinaryString(n);
assertEquals("111", binaryString);``````

## 4. Using Integer#toString Method

Now, let’s look at the signature of the Integer#toString method:

``public static String toString(int i, int radix)``

The Integer#toString method is an in-built method in Java that takes two arguments. First, it takes an integer that is to be converted to a string. Second, it takes radix that is to be used while converting the integer into its string representation.

It returns a string representation of the integer input in the base specified by the radix.

Let’s use this method to convert an integer into its binary format using a radix value of 2:

``````int n = 7;
String binaryString = Integer.toString(n, 2);
assertEquals("111", binaryString);``````

As we can see, we passed the radix value of 2 while calling the Integer#toString method to convert the integer n into its binary string representation.

## 5. Getting the N-Bit (Zero-Padded) Binary Representation

We’ve learned how to get an integer’s binary format string. Sometimes, we’d like to have a 4-bit, 8-bit, or 16-bit binary representation. For example, here’s what the integer 7 (111) ‘s 4-, 8- and 16-bit representations look like:

• 4-bit: 0111
• 8-bit: 00000111
• 16-bit: 0000000000000111

Next, let’s take the 8-bit format as an example to see how to get the binary string in the required format.

One way to achieve that is to use the String#format() function to format the binary string into a space-padded string. Then, we replace each space character with a 0:

``````int n = 7;
String binaryString = String.format("%8s", Integer.toBinaryString(n)).replace(" ","0");
assertEquals("00000111", binaryString);``````

Some popular libraries also offer the padding helper methods. For example, the StringUtils class from Apache Commons Lang 3 provides the leftPad() method, which allows us to left-pad a string with specified characters easily:

``````int n = 7;
String binaryString = StringUtils.leftPad(Integer.toBinaryString(n), 8, "0");
assertEquals("00000111", binaryString);``````

## 6. Conclusion

In this article, we looked at integer to binary conversion. Furthermore, we saw a couple of built-in Java methods to convert an integer into a string in binary format.

As always, all these code samples are available over on GitHub.

### Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE