Convert Decimal into Binary

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. 

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.*;
public class DecimalToBinary{
  public static void main(String args[]) throws IOException{
  BufferedReader bf = new BufferedReader(new InputStreamReader(;
  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:
Binary: 1111011
Share on Google+Share on Google+

Convert Decimal into Binary

Posted on: May 31, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: Convert Decimal into Binary   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 6, 2011
how to convert music in binary form

sir, please tell me that how to convert a music in binary code by java program. if any solution of problem than please send solution on my email id.
September 9, 2011
converting any base number

ahm how do you covert binary to hexa using the JOptonpane? or the javax.swing
September 17, 2011
how we can decimal into binary ?

tell me
Ben Dover
November 29, 2011
Im coming out of the closet

Ive decided to come out of the closet and tell everybody reading this tutorial that IM GAY!!!!
December 17, 2011

#include <stdio.h> #include <conio.h> #include <string.h> void main() { char str[10],temp; int i,len; printf("Enter String : "); scanf("%s",str); len=strlen(str)-1; for(i=0;i<strlen(str)/2;i++) { temp=str[i]; str[i]=str[len]; str[len--]=temp; } printf("%s",str); getch(); }
vinod kumarumar
February 10, 2012
programing Code

I want to make a programe "BinaryTo Decimal" But not used any key.Only used 0,1. HOW? please help me.
February 18, 2012
binary to decimal program

this program is really helpful
tara arif hussain
May 2, 2012

write program
October 12, 2012
object oriented programing

can you help me on how to program using tree and dequeue?