30+ Java Binary to Decimal Converter Examples

This post contains a total of 30+ Java Binary to Decimal Converters with Source Codes. All the Binary to Decimal converters are made using Java Programming language.

You can use the source codes of these programs for your own personal or educational use with credits to the original owner.

Related Posts

Click a Code to Copy it.

1. By Sarthak Mathur

Made by Sarthak Mathur. A simple Java Binary to decimal converter program. ( Source )

//insert a binary number or it would show an error message
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
        Scanner bi= new Scanner(System.in);
        long binarynum=bi.nextLong();
        boolean isBinary=true;
        String binarynum_str= ""+binarynum;
        for(int j=0; j<binarynum_str.length(); j++)
        {
           if(!(binarynum_str.charAt(j)=='0' || binarynum_str.charAt(j)=='1'))
               isBinary=false;
        }
        if(isBinary)
        {
        long decimal = binary_decimal(binarynum);
        System.out.println(binarynum+" in decimal form is "+decimal+".");
        }
        else
        {
            System.out.println("Please enter a binary number (A number which contains only 0 and 1).");
        }
    }
    static long binary_decimal(long a)
    {
        long f=a%10;
        long sum=0;
        for(long g=0;a!=0;f=a%10)
        {
            sum=sum+ (int)(f* Math.pow(2,g));
            a/=10;
            g++;
        }
        return sum;
    }
}
//insert a binary number or it would show an error message

2. By Muhd Khairul Amirin

Made by Muhd Khairul Amirin. ( Source )

public class Convert
{

static void p(String sl){
    System.out.println(sl);
}
    public static void main(String[] args) {
    
    
    //enter a binary
 
 p("           ************");
 p("           **Converter*");
 p("           ************");
 
 
 try{
 
    
 String a=new java.util.Scanner(System.in).nextLine();
 
 int b=Integer.parseInt(a,2);
 
  System.out.print("Enter a binary Input: "+a+"\n"+"Decimal form: "+b+"\n"+"Octal form "+Integer.toOctalString(b)); }
  
 catch (java.util.InputMismatchException e){
     p("Enter a binary");
 }
 
 catch (Exception e){
     p("Enter a binary");
 }
  
  
  
  
        
    }
}

3. By Atul

Made by Atul. ( Source )



import java.util.*;
public class Program
{
    public static void main(String[] args) {
       Scanner sc=new Scanner (System.in) ;
       int n=sc.nextInt ();
       System.out.println ("Enter a binary number="+n);
       int s=0;
       int i=0;
       while(n>0){
           int d=n%10;
           s=s+d*(int)(Math.pow(2,i));
           n=n/10;
           i++;
       }
       System.out.println ("Decimal version of that number="+s);
    }
}

4. By Vaishnavi Kumari

Made by Vaishnavi Kumari. ( Source )


import java.util.Scanner;

public class Binary_Decimal {
Scanner scan;
int num;

void getVal() {
System.out.println("Binary to Decimal");
scan = new Scanner(System.in);

System.out.println("\nEnter the number :");
num = Integer.parseInt(scan.nextLine(), 2);
}

void convert() {
String decimal = Integer.toString(num);
System.out.println("Decimal Value is : " + decimal);
}
}

class MainClass {
public static void main(String args[]) {
Binary_Decimal obj = new Binary_Decimal();

obj.getVal();
obj.convert();
}
}

5. By Raphael Williams

Made by Raphael Williams. ( Source )

import java.util.Scanner;

public class BinaryToDecimal
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter a binary number:");
        String binary = sc.nextLine();
        System.out.println("Output: "+ Integer.parseInt(binary, 2));
    }
}

6. By Bikash Sah

Made by Bikash Sah. ( Source )

import java.util.Scanner;
public class Program
{
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        System.out.println("Entered No. is = "+n);
        int a,d,c;
        c=0;
        d=0;
        while(n!=0)
        {
            a=n%10;
            d=d+a*(int)Math.pow(c,2);
            n=n/10;
            c++;
        }
        System.out.println("Converted no. is = "+d);
    }
}

7. By Bipin Tatkare

Made by Bipin Tatkare. ( Source )

import java.util.*;
import java.lang.Math;
public class Program
{
    public static int power(int x){
        
        double num = Math.pow(2, x);
        int get = (int)num;
        return get;
        
    }
    
    
	public static void main(String[] args) {
		
		Scanner input = new Scanner(System.in);
		int bin = input.nextInt();
		int inc = 0;
		int smart = 0;
		int copy = bin;
		while(copy > 0){
		    
		    int dig = copy % 10;
		    int func = power(smart);
		    inc = inc + dig * func;
		    smart++;
		    copy /= 10;
		    
		}
		
		System.out.println(inc);
		
	}
}

8. By matheus

Made by matheus. This code converts your binary number to a decimal number! ( Source )



import java.util.Scanner;
public class Binary_Decimal {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        ConverterCode converterCode = new ConverterCode();

        String binary = in.next(); // here, the user input the binary number!
        String decimal = converterCode.decimalNumber(binary);

        if (converterCode.isOk == true && converterCode.size <= 19) {

            System.out.println("Your binary number: " + binary);
            System.out.println("Decimal: " + decimal);

        }else{
            
            System.out.println("Error: " + decimal);

        }
        System.out.println("\n\nThanks!\nMatheus Rambo!");
    }
}

class ConverterCode {

    protected boolean isOk = false;
    protected int size = 0;
    
    protected String decimalNumber(String binary) {

        isOk = verifyElements(binary); // checks if the user inputed just the 0 or 1!
         size = binary.length();
        
         if( size > 19 && isOk == true){
            
            return "Number limit exceded!";
            
        } else if(isOk == false) {
            // if don't pass!
            return "Invalid binary number!!!";

        }else{
         
        // if the user inputed a valid binary number
        return converterBin(binary);
        }
         
    }

    private boolean verifyElements(String binary) {

        for (int i = 0; i < binary.length(); i++) {

            char element = binary.charAt(i);

            if (element != '1' && element != '0') {
                // case the user input a 5 or a letter!
                return false;

            }

        }
        return true;
    }

    private String converterBin(String binary) {

        long value = 0;

        int exec;
        int times = 0;

        for (int i = binary.length() - 1; i >= 0; i--) {

            int actual = Integer.parseInt(binary.substring(i, i + 1));

            exec = 0;

            int x = 1;

            if (actual != 0 & times != 0) {

                for (; exec < times; exec++) {
                    // I could use the method pow(); but I dont want to use math Libs!
                    x = x * 2;

                }
                value += x;

            }
            if (times == 0) {
                if (actual == 1) {
                    value += 1;
                }
            }

            ++times;

        }
        return String.valueOf(value);

    }
}

9. By Luis GarcΓ­a

Made by Luis GarcΓ­a. ( Source )

public class Program
{
    public static void main(String[] args) {
        Converter c = new Converter(110011);
        System.out.println(c.toDecimal());
        System.out.println(c.toOctal());
    }
    
    private static class Converter
    {
        private String binary;
        
        public Converter(String binary) {
            this.binary = binary;
        }
        
        public Converter(int binary) {
            this.binary = String.valueOf(binary);
        }
        
        public int toDecimal() {
            int x = 0;
            try {
                x = Integer.parseInt(binary, 2);
            } catch (NumberFormatException nfe) {
                System.out.println("An error ocurred\n" + nfe.getMessage());
                System.exit(0);
            }
            
            return x;
        }
        
        public String toBinary() {
            return binary;
        }
        
        public String toOctal() {
            return Integer.toOctalString(toDecimal());
        }
    }
}

10. By Prantik Sarkar

Made by Prantik Sarkar. ( Source )

import java.util.Scanner;
public class BinToDec_ViceVersa
 {
static int convertBinaryToDecimal(long num)
    {
        int decimalNumber = 0, i = 0;
        long remainder;
        while (num != 0)
        {
            remainder = num % 10;
            num /= 10;
decimalNumber += remainder * Math.pow(2, i);
            ++i;
        }
        return decimalNumber;
    }
static void convertDecimalToBinary(int n)
    {
        long binaryNumber = 0;
        int remainder, i = 1, step = 1;
        int temp=n;
        while (n!=0)
        {
            remainder = n % 2;
            n /= 2;
        binaryNumber += remainder * i;
            i *= 10;
        }
System.out.println("The binary equivalent of "+ temp + " automatically tested  " + binaryNumber );
}
    public static void main(String[] args)
 {
System.out.println(" Please enter any number in binary example: 10000 to convert it to decimal equivalent example : 32 and cross check decimal to binary automatically: ");
Scanner sc = new Scanner(System.in);   long num = sc.nextLong();                        
int decimal = convertBinaryToDecimal(num);
System.out.println( num + " binary is " + decimal + " in decimal " );
convertDecimalToBinary(decimal);
    }
}

11. By Shamoil Arsiwala

Made by Shamoil Arsiwala. ( Source )

import java.util.*;
public class B2D
{
    static int choice, v, d, i;
    static char ch;
    static String b = "", s;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
            System.out.println("Enter Binary Number");
            s = sc.nextLine();
            //System.out.println(s);
            B2Df(s);
            
            }
    public static void B2Df(String v)
    {
        double t = 0.0;
        int w = 0;
        //String a = Integer.toString(v);
        int q = v.length() - 1;
        for (i = 0; i <= q; i++)
        {
            
            t = Math.pow(2, i);
            ch = v.charAt(q - i);
            w = (int)(ch - 48);
            d += (t * w);
        }
        System.out.println("Decimal value for " + v + " is " + d);
    }
}

12. By Viki

Made by Viki. ( Source )

/*Binary to decimal*/


import java.util.Scanner;
public class bin2dec
{
    public static void main(String[] args) 
    
    {
       Scanner s = new Scanner(System.in);
       System.out.println("Enter the number: ");
       int num = s.nextInt();
       
      int decimal = 0; int p = 0;
      int temp = 0; 
      
       while(num!=0)
       {
           temp = num%10;
           decimal =(int) (decimal+temp*Math.pow(2,p));
           num = num/10;
           p++;
       }
       System.out.println("The converted decimal number from binary is: "+decimal);
        
    }
}

13. By Guntas Saran

Made by Guntas Saran. Write a Binary Number which you want to convert in Decimal Example:input:100110 Output:38. ( Source )


import java.util.*;
public class Program
{
    public static void main(String[] args) 
    {
        Scanner sc=new Scanner(System.in);
        long b=sc.nextLong();
        long c=b;
        double d=0,p=0;
        while (b!=0)
        {
            d+=((b%10)*Math.pow(2,p));
            p=p+1;
            b=b/10;
        }
        System.out.println("The Decimal Number of "+c+" is: "+(int)d);
System.out.println("Please like It Thanks!!!");
            }
}

14. By ÁÑÀÑYÁÀBHÎK

Made by ÁÑÀÑYÁÀBHÎK. ( Source )

import java.util.Scanner;
public class Binary
{
   public static void main(String[]args)
   {
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter a binary number");
      int n=sc.nextInt();
      int f=0,s=0,c=0;
      while(n!=0)
      {
         int d=n%10;
         if(d!=1&&d!=0)
         {
            f=1;
            break;
             
         }
         s=s+d*(int)(Math.pow(2,c));
         c++;
         n=n/10;
      }
      if(f==0)
      System.out.println("Decimal number="+s);
      else
      System.out.println("Not a binary number");
   } 
}

15. By Boris Batinkov

Made by Boris Batinkov. ( Source )

/*
 * Binary to Decimal converter
 */

import java.util.Scanner;

public class Program
{
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        String binNum = scan.nextLine();
        binNum = binNum.replaceAll("[^0-1]", "");
        
        int decNum = 0;
        
        for (int i = binNum.length() - 1, j = 0; i >= 0; i--, j++) {
            decNum += Integer.parseInt("" + binNum.charAt(i)) * 1 << j;
        }
        
        System.out.println(binNum);
        System.out.println(decNum);
    }
}

16. By Utkarsh Mishra

Made by Utkarsh Mishra. ( Source )

//CODED BY M.UTKARSH...
import java. util. Scanner;
public class BinaryToDecimal {
public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
    System.out.println("Enter the binary number to be converted to decimal"); int C=0;
    int input=sc.nextInt(); double sum=0.0;
    String tmp=""+input;
    int len=tmp.length();
    while(input>0)
    {
      int rem=input%10;
      sum=sum+(Math.pow(2,C)*rem);
        input=input/10;
        C++;
      }
      System.out.println("The decimal conversion is :"+" " + sum);
sc.close();
    }
    }

17. By Trent

Made by Trent. ( Source )

/*  Write out a binary 
 *  number and it will
 *  be converted to 
 *  decimal (101 = 5)
 */
import java.util.Arrays;
import java.util.Scanner;

public class toBinary
{
    //main method prints out statements and puts the array back in the right order
    public static void main(String[] args)
    {
        //System.out.println("Write a binary number (1's and 0's) to be converted into decimal.");
        Scanner sc = new Scanner(System.in);                    //gathers the users input
        int[] binaryNumbers = assignValue(sc);                  //sends the users input to assignValue for assign
        int answer = binary(binaryNumbers);
        
        for (int i = 0; i < binaryNumbers.length/2; ++i)        //reverses the elements of the array so that the print statement will work
        {
            int tempArray = binaryNumbers[i];
            binaryNumbers[i] = binaryNumbers[binaryNumbers.length -i -1]; 
            binaryNumbers[binaryNumbers.length -i -1] = tempArray;
        }
        //prints out the sum and removes the excess info
        System.out.println(Arrays.toString(binaryNumbers).replace(", ", "").replace("[", "").replace("]", "") + " is " + answer + " in decimal");
    }
    //lets the user assign values to the array then return it to the main
    public static int[] assignValue(Scanner keyboard)
    {
        String arrayNumber = keyboard.nextLine();           //Gets the number from the scanner and assigns it to the string
        int arraySize = arrayNumber.length();               //gets the size of the array from the length of the string
        char[] digits = arrayNumber.toCharArray();          //converts the string to an array
        int[] binaryArray = new int[arraySize];             //declares an empty array whose size will be set by arraySize
        
        for (int i = 0; i < binaryArray.length; ++i)        //cycles through the array and assigns values from the scanner
        {
            binaryArray[i] = (int) digits[i] - 48;
        }
        
        return binaryArray;                                 //returns number to main to then be sent to the binary method
    }
    //calculates the binary numbers value in decimal
    public static int binary(int[] array)
    {
        int sum = 0;                                    //declare and initialize the sum
        
        for (int i = 0; i < array.length/2; ++i)        //reverses the elements of the array so that base conversion will work
        {
            int tempArray = array[i];
            array[i] = array[array.length -i -1]; 
            array[array.length -i -1] = tempArray;
        }

        for (int i = 0; i < array.length; ++i)           //converts the numbers from base 10 to base 2 (binary)
        {
            int power = (int) Math.pow(2, i);
            sum += (array[i] * power);
        }       
        return sum;                                     //returns the sum so it can be printed
    }
}

18. By Aditya Negi

Made by Aditya Negi. ( Source )


import java.util.*;
import java.lang.*;
public class Program
{
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner (System.in);
		String b = sc.nextLine();
		System.out.println("Entered binary value: "+b);
        int decimal = Integer.parseInt(b,2);
        System.out.println("Decimal form for "+b+" is "+decimal);
	}
}

19. By Shagun Pandey

Made by Shagun Pandey. ( Source )

class Main {
    
  public static void main(String[] args) {

    // binary number
    long num = 1101101;

    // call method by passing the binary number
    int decimal = convertBToD(num);

    System.out.println("Binary to Decimal");
    System.out.println(num + " = " + decimal);
  }

  public static int convertBToD(long num) {
    int decimalNumber = 0, i = 0;
    long remainder;
    
    while (num != 0) {
      remainder = num % 10;
      num /= 10;
      decimalNumber += remainder * Math.pow(2, i);
      ++i;
    }
    
    return decimalNumber;
  }
}

20. By Manish Soni

Made by Manish Soni. ( Source )

/*Binary to decimal */
public class BinaryDecimal {

public static void main(String[] args) {

long num = 1101101;

int decimal = convertBinaryToDecimal(num);

System.out.print(num+" in binary = "+decimal+" in decimal");

}

public static int convertBinaryToDecimal(long num)

{
int decimalNumber = 0, i = 0;

long remainder;

while (num != 0)
{
remainder = num % 10;
num /= 10;
decimalNumber += remainder * Math.pow(2, i);
++i;

}
return decimalNumber;
}
}

21. By Akshaya

Made by Akshaya. ( Source )


import java.util.Scanner;
public class BintoDec {
	public static void main(String [] args){
		int Binnum,decnum=0,rem,i=1;
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter An Binary Number");
		Binnum =sc.nextInt();
		while(Binnum!=0)
		{
			rem=Binnum%10;
			decnum=decnum+rem*i;
			i=i*2;
			Binnum=Binnum/10;
		}
		System.out.println("Decimal value is :");
		System.out.println(decnum);
	}
}

22. By joyce villaraza

Made by joyce villaraza. ( Source )

import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
      
      Scanner f = new Scanner (System.in);
      
        long b ;
        
        b=f.nextLong();
        
         int d = a(b); 
         
         System.out.printf(" The decimal is: "+ d);
          }
   public static int a (long b) 
          {
           int c = 0, i = 0;
           long  e;
           while (b != 0) 
          { 
             e= b % 10; b /= 10; 
              c += e * 
            Math.pow(2, i); ++i; 
          } 
          return  c;
    }
}

23. By Manas Patel

Made by Manas Patel. ( Source )

//write binary number as input
import java.util.Scanner;
public class Program
{
    public static void main(String[] args) {
        Scanner bi= new Scanner(System.in);
        long binarynum=bi.nextLong();
        boolean isBinary=true;
        String binarynum_str= ""+binarynum;
        for(int j=0; j<binarynum_str.length(); j++)
        {
           if(!(binarynum_str.charAt(j)=='0' || binarynum_str.charAt(j)=='1'))
               isBinary=false;
        }
        if(isBinary)
        {
        long decimal = binary_decimal(binarynum);
        System.out.println(binarynum+" in decimal form is "+decimal+".");
        }
        else
        {
            System.out.println("Please enter a binary number (A number which contains only 0 and 1).");
        }
    }
    static long binary_decimal(long a)
    {
        long f=a%10;
        long sum=0;
        for(long g=0;a!=0;f=a%10)
        {
            sum=sum+ (int)(f* Math.pow(2,g));
            a/=10;
            g++;
        }
        return sum;
    }
}

24. By Bhupendra Pratap

Made by Bhupendra Pratap. A simple java program to convert binary number to decimal number. ( Source )

import java.util.*;
public class Program
{
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
        int n=0,c=0;
        for(int i=s.length()-1;i>=0;i--)
        {
           if(s.charAt(i)=='1') 
           n=n+(int)Math.pow(2,c);
           c++;
        }
        System.out.println(n);
    }
}

25. By Redwin Calzado

Made by Redwin Calzado. ( Source )

import java.util.*;

public class Program
{
	static Scanner sc = new Scanner(System.in);
	public static void main(String[] args) {
		String in[] = sc.nextLine().split("|");
		//displaying input using loop
		for(int a = 0; a < in.length; a++){
			System.out.print(in[a]);
		}
		System.out.println();
		int val[] = new int[in.length];
		int bin = 1;
		//generating values for binary
		for(int r = val.length-1;r>=0;r--){
			
			val[r]=bin;
		//System.out.println(val[r]);
			bin = bin * 2;
		}
		
		//adding the values
		int decVal = 0;
		for(int c = in.length-1;c >= 0;c--){
			
			if(in[c].equals("1")){
				decVal = decVal + val[c];
			}
		}
		
		System.out.println("Decimal Value =" + decVal);
	}
}

26. By Aadarsh velu

Made by Aadarsh velu. ( Source )

//Binary to Decimal Calculator.

//copy the code and run in your compiler to run the program in right way.


import java.util.Scanner;

public class Program
{
	public static void main(String[] args) {
	    
	    Scanner sc = new Scanner (System.in);
	    
		System.out.println( " Enter an Binary  : " );

        String n = sc.next();

        System.out.println( " Answer is : " +Integer.parseInt(n , 2));
        
	}
}

27. By Shahbaz M.R

Made by Shahbaz M.R. ( Source )

import java.util.Scanner;
public class BnDc
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
long num;
num=sc.nextLong();

int decimal=convertBnToDc(num);
System.out.printf("%d in binary=%d in decimal",num,decimal);
}
public static int convertBnToDc(long num)
{
int decimalNum=0,i=0;
long remainder;
while(num!=0)
{
remainder=num%10;
num=num/10;
decimalNum+=remainder*Math.pow(2,i);
++i;
}
return decimalNum;
}
}

28. By Dima

Made by Dima. ( Source )

import java.util.Scanner ;
public class DecimaltoBinary
{
	public static void main(String[] args) {
		
		System.out.println("enter decimal value:");
		
		Scanner sc = new Scanner (System.in);
		int num = sc.nextInt();
		String Buff = "";
		
		while(num>0){
		    int ternary=(num>1)?(num%2):1;
		    
		    Buff = ternary + Buff ;
		    num = num/2;
		}
		System.out.print(Buff);
	}
}

29. By Mahtab Alam

Made by Mahtab Alam. ( Source )

import java.util.Scanner;
public class dec {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		long bin, dec = 0, rem, num, base = 1;
		Scanner s = new Scanner(System.in);
	
		num= s.nextInt();
		bin=num;
		while (num > 0)
	    {
	        rem = num % 10;
	        dec = dec + rem * base;
	        base = base * 2;
	        num = num / 10;
	    }
		System.out.println("The decimal number is= "+dec);
		return;

	}

}

30. By Ashok Sirvi

Made by Ashok Sirvi. To convert Binary number to decimal number use, int parseInt method of Integer wrapper class. Pass 2 as redix second argument. ( Source )

import java.io.*;

class BinaryToDecimal
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter Binary no. to convert in Decimal : ");
        String number = br.readLine();

        int decimalNumber = Integer.parseInt(number, 2);
        System.out.println("Binary number converted to decimal number");
        System.out.println("Decimal number is : " + decimalNumber);

    }
}

31. By Kalpit Patil

Made by Kalpit Patil. Java Program to convert Binary Number to its Equivalent Decimal Form. ( Source )

/** 
 * Eg 0: 1000101 --> 69
 * Eg 1: 1011 --> 11
 * Eg 2: 1100011 --> 99
*/
import java.util.*;
class BtoD
{
    int n;
    int t;
    int d;
    int c;
    BtoD()
    {
        n=0;
        t=0;
        d=0;
        c=0;
    }
    void input()
    {
        Scanner pk=new Scanner(System.in);
        System.out.println("Enter a Binary Number.");
        n=pk.nextInt();
        t=n;
    }
    void compute()
    {
        while(t>0)
        {
            int r=t%10;
            d+=r*(int) Math.pow(2,c);
            c++;
            t/=10;
        }
    }
    void print()
    {
        System.out.println("Binary Number : "+n);
        System.out.println("Decimal Form : "+d);
    }
    public static void main(String args[])
    {
        BtoD kp=new BtoD();
        kp.input();
        kp.compute();
        kp.print();
    }
}