Home Java Java-conversion Convert Decimal into Binary



Convert Decimal into Binary
Posted on: May 31, 2007 at 12:00 AM
In this section, you will learn to convert decimal number into binary.

Convert Decimal into Binary 

     

In this section, you will learn to convert decimal number into binary. The java.lang package provides the functionality to convert  a decimal number into a binary number. 

Description of program:

This program takes a decimal number from console and it converts it  into binary format using the toBinaryString() method. Input number is taken as string  so convert it into an integer data using the Integer.parseInt() method. To convert the Decimal number into binary number use toBinaryString() method. 

toBinaryString():
This method takes an integer type value and returns its string representation of  integer values which represents the data in binary. The base of binary number is 2.

Here is the code of this program:

import java.lang.*;
import java.io.*;
public class DecimalToBinary{
  public static void main(String args[]) throws IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  System.out.println("Enter the decimal value:");
  String hex = bf.readLine();
  int i = Integer.parseInt(hex);  
  String by = Integer.toBinaryString(i);
  System.out.println("Binary: " + by);
  }
}  

Download this program.

Output of this program:

C:\corejava>java DecimalToBinary
Enter the decimal value:
123
Binary: 1111011
C:\corejava>_

Related Tags for Convert Decimal into Binary:
javacidefunctionbinaryconvertfuniohelpvidecimalnumberintidpackagefunctionalexampleunctoexamcilanbieitdesliiminconvertingmntpsjpackesfuncageintoproackbinxaxampsatpackhamplfunctionalityarrtvassthavhatpleplprono


More Tutorials from this section

Ask Questions?    Discuss: Convert Decimal into Binary   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.