Convert an integer to binary in Java
This post will discuss how to convert an integer to a 32-bit binary string in Java.
There are several ways to convert an integer to binary format in Java:
1. Using Integer.toBinaryString() method
A simple solution is to use the toBinaryString() method provided by the Integer wrapper class. It takes an integer to be converted to a string and return its string representation in binary format.
Note that the binary representation returned by toBinaryString() is not left-padded with zeros.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
class Main { public static String toBinary(int x) { return Integer.toBinaryString(x); } public static void main(String[] args) { System.out.println(toBinary(10000)); System.out.println(toBinary(-10000)); } } |
Output:
10011100010000
11111111111111111101100011110000
2. Using Apache Commons Lang
To pad the binary value with leading zeros to a specific length, we can use StringUtils class from Apache Commons Lang Library:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.StringUtils; class Main { public static String toBinary(int x) { return StringUtils.leftPad(Integer.toBinaryString(x), 32, '0'); } public static void main(String[] args) { System.out.println(toBinary(10000)); System.out.println(toBinary(-10000)); } } |
Output:
00000000000000000010011100010000
11111111111111111101100011110000
3. Using String.format() method
Another alternative to left pad the binary representation of an integer with zeros is to use the format() method of the String class. It can be used as follow:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
class Main { public static String toBinary(int x) { return String.format("%32s", Integer.toBinaryString(x)).replaceAll(" ", "0"); } public static void main(String[] args) { System.out.println(toBinary(10000)); System.out.println(toBinary(-10000)); } } |
Output:
00000000000000000010011100010000
11111111111111111101100011110000
4. Custom routine
We can also write our own routine to display the leading zeros using a StringBuilder or a char array.
⮚ Using StringBuilder:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Main { public static String toBinary(int x) { StringBuilder result = new StringBuilder(); for (int i = 31; i >= 0 ; i--) { int mask = 1 << i; result.append((x & mask) != 0 ? 1 : 0); } return result.toString(); } public static void main(String[] args) { System.out.println(toBinary(10000)); System.out.println(toBinary(-10000)); } } |
Output:
00000000000000000010011100010000
11111111111111111101100011110000
⮚ Using char array:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Main { public static String toBinary(int x) { final char[] buff = new char[32]; for (int i = 31; i >= 0 ; i--) { int mask = 1 << i; buff[31 - i] = (x & mask) != 0 ? '1' : '0'; } return new String(buff); } public static void main(String[] args) { System.out.println(toBinary(10000)); System.out.println(toBinary(-10000)); } } |
Output:
00000000000000000010011100010000
11111111111111111101100011110000
5. Using Integer.toString() method
Finally, the Integer class provides another built-in method, toString(), that returns a string representation of an integer in the specified radix. This doesn’t work with negative integers.
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static String toBinary(int x) { return Integer.toString(x, 2); } public static void main(String[] args) { System.out.println(toBinary(10000)); } } |
Output:
10011100010000
BigInteger has a similar method where we can specify the radix.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.math.BigInteger; class Main { public static String toBinary(int x) { BigInteger bg = new BigInteger(String.valueOf(x), 10); return bg.toString(2); } public static void main(String[] args) { System.out.println(toBinary(10000)); } } |
Output:
10011100010000
That’s all about converting an integer to binary in Java.
Also See:
Convert an integer to a binary string of a specific length in Java
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)