Java

How to Convert an Integer to Binary in Java

While dealing with mathematical computations in Java, there can be instances where the developer needs to work with the “binary numbers” since these numbers are mostly utilized in computer applications. Moreover, the “binary representation” is convenient in coding as it results in fewer computational errors. In such cases, converting an integer to binary in Java assists in streamlining the code functionalities at the developer’s end.

This article will discuss the approaches to convert an integer into binary using Java.

How to Convert/Transform an Integer to Binary Using Java?

An integer can be converted into binary by applying the following approaches:

Approach 1: Convert an Integer to Binary in Java Using “Integer.toBinaryString()” Method

The “Integer.toBinaryString()” method gives a string outcome of the integer argument in “base 2”. This method can be applied to simply transform the specified integer into binary.

Syntax

public static String toBinaryString(integer)

In the above syntax, “integer” corresponds to the integer that needs to be converted into binary.

Example

Let’s overview the below-provided example:

int convertVal = 98;

System.out.println("The integer value is: "+convertVal);

System.out.println("The converted binary value is: "

+ Integer.toBinaryString(convertVal));

Apply the following steps as given in the above code:

  • Firstly, initialize an integer value that needs to be converted into binary format and display it.
  • After that, simply invoke the “Integer.toBinaryString()” method to transform the integer value specified as its(method) parameter into binary and display it.

Output

In this output, it is evident that the corresponding binary value is computed and displayed.

Approach 2: Convert an Integer to Binary in Java Using “Integer.toString()” Method

The “Integer.toString()” method is utilized to give a string object referring to the “Number Object” value. This method can be implemented to transform the provided integer into binary by specifying the base to be converted in.

Syntax

Integer.toString(int num, int radix)

In the above-given syntax:

  • num” refers to the number that needs to be converted into binary.
  • radix” corresponds to the resultant base.

Example

Go through the below-given example to understand the discussed concept:

int convertVal = 72;

System.out.print("The integer value is:"+ convertVal);

System.out.print("\nThe converted binary value is:"+Integer.toString(convertVal, 2));

In the above lines of code:

  • Likewise, initialize an integer value and display it.
  • Now, apply the “Integer.toString()” method by specifying the number and the base to be converted as its(method) parameters, respectively.
  • This will resultantly convert the specified integer into binary.

Output

As you can see that the number is converted into “base 2” accordingly.

Approach 3: Convert an Integer to Binary in Java Using “Long Division” Method

Long Division” approach can be utilized to simply divide the given number, append the left out remainder in an array and display the resultant binary by iterating through the “remainder” values in an array in a reverse manner.

Example

Overview the following illustration to clear out the concept:

int convertVal= 98;

System.out.print("The integer value is:" +convertVal);

int number[] = new int[32];

int index=0;

while(convertVal>0){

number[index++]=convertVal%2;

convertVal=convertVal/2;

}

System.out.print("\nThe converted binary value is:");

for(int i=index-1; i>=0; i--){

System.out.print(number[i]);

}

In the above demonstration:

  • Recall the discussed approaches for initializing and displaying an integer value.
  • Now, declare an array of int of size “32” considering the 32-bit binary representation and allocate the value of the array index, in the next step.
  • After that, divide the provided number by “2” such that the left-out values are placed in the declared array considering the exception in the “while” loop.
  • Lastly, iterate through the array values in a reverse manner and display the values via the “for” loop.
  • “for” Loop Explanation: Initiating from “index – 1” since the index starts from “0” and reverse iteration is carried out since the binary representation is computed in such a manner.

Output

The resultant outcome indicates that the desired requirement is fulfilled.

Conclusion

An integer can be converted into binary by applying the “Integer.toBinaryString()” method, the “Integer.toString()” method, or the “Long Division” approach. These approaches can be applied to convert the specified integer into binary directly, by specifying the base to be converted in, or by dividing the provided integer by “2”, placing the remainder values in an array, and iterating through them, respectively. This blog elaborated on the approaches to convert an integer to binary in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.