Java convert binary to decimal

Let’s learn java convert binary to decimal.

Java convert binary to decimal

Program to convert binary to decimal is one of the common interview question.

In the below example to convert binary to decimal is first user enters binary number using nextLine() method of Scanner class.

java convert binary to decimal

This binary number is stored in String variable ‘strBinary’.

In the next step each binary digit is multiplied with power of 2a and added.

Where ‘a’ is position of binary number from right side.

Here we are using pow() method of class Math.

Now let’s learn java convert binary to decimal or binary to decimal java while loop.

import java.util.Scanner;
public class BinaryToDecimalDemo 
{
   public static void main(String[] args) 
   {
      int number, decimal = 0, a = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter binary number: ");
      String strBinary = sc.nextLine();
      number = Integer.parseInt(strBinary);
      while(number != 0){
         decimal += (number % 10) * Math.pow(2, a);
         number = number / 10;
         a++;
      }
      System.out.println("Decimal number: " + decimal);
      sc.close();
   }
}


Output:

Please enter binary number : 1111
Decimal number : 15


Also read – comments in java


Now let’s learn to convert binary to decimal conversion in two ways, one is using Integer.parseInt() method and another using custom logic.

First let’s learn binary to decimal conversion using custom logic.

// using while loop
public class BinaryToDecimalExample
{
   public int convertToBinary(int binary)
   {
      int decimal = 0;
      int b = 0;
      while(true)
      {
         if(binary == 0)
         {
            break;
         }
         else
         {
            int temp = binary % 10;
            decimal += temp * Math.pow(2, b);
            binary = binary / 10;
            b++;
         }
      }
      return decimal;
   }
   public static void main(String[] args)
   {
      BinaryToDecimalExample obj = new BinaryToDecimalExample();
      System.out.println("To decimal : " + obj.convertToBinary(110111));
   }
}


Output:

To decimal : 55


Binary to decimal conversion using inbuilt function

Here let’s see program for binary to decimal conversion using Integer.parseInt() method.

public static int parseInt(String s, int radix) throws NumberFormatException


Integer.parseInt() method takes two arguments string and radix to which we have to convert the number.

Also read – continue statement in java

Integer.parseInt() method returns the integer represented by the string argument in the specified radix.

This method parses the string argument as a signed integer in the radix specified by the second argument.

import java.util.*;
public class BinaryToDecimalDemo
{
   public static void main(String[] args)
   {
      String strBinary = "110111";
      int decimal = Integer.parseInt(strBinary, 2); 
      System.out.println(decimal);
   }
}


Output:

55


Click here for vice versa of above program.


program to convert binary to decimal using recursion

Now let’s learn recursive program to convert binary to decimal.

public class BinaryToDecimalRecursion
{
   static int binaryToDecimal(String strBinary, int a) 
   {
      int size = strBinary.length(); 
      if(a == size - 1)
         return strBinary.charAt(a) - '0'; 
      return((strBinary.charAt(a) - '0') << (size - a - 1)) + binaryToDecimal(strBinary, a + 1); 
   }
   public static void main(String[] args) 
   {
      String binaryNumber = "1111"; 
      int n = 0;
      System.out.println("Convert binary to decimal using recursion: ");
      System.out.println(binaryToDecimal(binaryNumber, n));
   }
}


Output:

Convert binary to decimal using recursion: 15