Q:

Java Convert Decimal to Binary

belongs to collection: Java Conversion Programs

0

We can convert decimal to binary in java using Integer.toBinaryString() method or custom logic.

Java Decimal to Binary conversion: Integer.toBinaryString()

The Integer.toBinaryString() method converts decimal to binary string. The signature of toBinaryString() method is given below:

 

public static String toBinaryString(int decimal)  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Let's see the simple example of converting decimal to binary in java.

public class DecimalToBinaryExample1{  
public static void main(String args[]){  
System.out.println(Integer.toBinaryString(10));  
System.out.println(Integer.toBinaryString(21));  
System.out.println(Integer.toBinaryString(31));  
}}  

 

Output:

1010
10101
11111

Java Decimal to Binary conversion: Custom Logic

We can convert decimal to binary in java using custom logic.

public class DecimalToBinaryExample2{    
public static void toBinary(int decimal){    
     int binary[] = new int[40];    
     int index = 0;    
     while(decimal > 0){    
       binary[index++] = decimal%2;    
       decimal = decimal/2;    
     }    
     for(int i = index-1;i >= 0;i--){    
       System.out.print(binary[i]);    
     }    
System.out.println();//new line  
}    
public static void main(String args[]){      
System.out.println("Decimal of 10 is: ");  
toBinary(10);    
System.out.println("Decimal of 21 is: ");  
toBinary(21);    
System.out.println("Decimal of 31 is: ");    
toBinary(31);  
}}    

 

Output:

Decimal of 10 is: 
1010
Decimal of 21 is: 
10101
Decimal of 31 is: 
11111

  

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Convert Hexadecimal to Decimal... >>
<< Java Convert Binary to Decimal...