tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Core > Integer > Decimal to Binary Conversion

Decimal to Binary Conversion 

The following example shows how to convert a number in Decimal system to Binary system and vice versa.

File Name  :  
com/bethecoder/tutorials/core/integer/Integer2Binary.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.core.integer;

public class Integer2Binary {

  /**
   @param args
   */
  public static void main(String[] args) {

    int num = 21;
    String str = Integer.toBinaryString(num);
    System.out.println(num + "(decimal) in binary : " + str);
    
    num = Integer.parseInt(str, 2);
    System.out.println(str + "(binary) in decimal : " + num);
    
  }

}
   

It gives the following output,
21(decimal) in binary : 10101
10101(binary) in decimal : 21



 
  


  
bl  br