Convert Decimal to Binary in Java

Java 8Object Oriented ProgrammingProgramming

Use Integer.toBinaryString() method to convert Decimal to Binary in Java.

Firstly, set a decimal value.

int val = 300;

Now use the Integer.toBinaryString() method to perform conversion on the above given decimal value.

String res = Integer.toBinaryString(val);

Let us see the complete example.

Example

 Live Demo

public class Demo {
   public static void main( String args[] ) {
      int val = 300;
      System.out.println("Decimal = "+val);
      String res = Integer.toBinaryString(val);
      System.out.println("Binary = "+res);
   }
}

Output

Decimal = 300
Binary = 100101100
raja
Published on 13-Nov-2018 07:26:10
Advertisements