I am going to discuss three different ways to convert binary to decimal in java.
  • Using the built-in Integer.parseInt() method
  • Building our own logic when the binary number is given as a String variable
  • Building our own logic when the binary number is given as an int variable

Basic Concept of Conversion

Let's say we have a binary number 1011

1. Multiply the right most bit with two to the power zero and write the answer.

2. Move to the left while increasing the power of two. Write the answer for each bit.

3. Add up all the answers to get the decimal number.

It is important to keep these steps in the mind because they will be required to solve the problem.

Program

Using the built-in Integer.parseInt() method

Here we will use the static method Integer.parseInt(String s, int radix) which takes two arguments. The first argument is a String and the second is the radix (base) in which the number is to be converted. To convert from binary to decimal, we will pass the 2 as radix and the string will be our binary number. See the official documentation for the function here.
  1.  public class BinaryToDecimal {
  2.  
  3.      public static void main(String[] args) {
  4.          String bin1="1110110",bin2="11110",bin3="1011";
  5.          System.out.println(bin1+" = "+toDecimal1(bin1));
  6.          System.out.println(bin3+" = "+toDecimal1(bin2));
  7.          System.out.println(bin3+" = "+toDecimal1(bin3));
  8.      }
  9.  
  10.      public static  int toDecimal1(String binary){
  11.          return Integer.parseInt(binary,2);
  12.      }
  13.  
  14.  }

Output


1110110 = 118
1011 = 30
1011 = 11

Custom Logic (with binary number as String)

If we have the binary number in the form of a String, code will be

  1.  public class BinaryToDecimal {
  2.  
  3.      public static void main(String[] args) {
  4.          String bin1="1110110",bin2="11110",bin3="1011";
  5.          System.out.println(bin1+" = "+toDecimal2(bin1));
  6.          System.out.println(bin3+" = "+toDecimal2(bin2));
  7.          System.out.println(bin3+" = "+toDecimal2(bin3));
  8.      }
  9.  
  10.      public static int toDecimal2(String binary){
  11.          char[] c=binary.toCharArray();
  12.          int len=c.length;
  13.          int decimal=0;
  14.          for (int i=0;i<len;i++){
  15.              int bit=c[i]-'0';
  16.              int power=len-i-1;
  17.              decimal+=bit * Math.pow(2, power);
  18.          }
  19.          return decimal;
  20.     }
  21.  
  22.  }


Output


1110110 = 118
1011 = 30
1011 = 11

Explanation

  • First, we need to break the String to get the bits. We did this by calling the toCharArray() function on the String.
  • After breaking the String, we extracted the bits in int form by subtracting the character '0' from each character of the String.
  • The character array counts the indices from the left. But we want to count from right to left to get the power of two for conversion. So we used power = len - i - 1 to get the equivalent index form the right.
  • The result of each bit * 2^power is added to the int variable decimal which is initialized to zero. After the loop ends, we have the result in the variable decimal.

Custom Logic (with binary number as int)

If we have the binary number as int variable, we will use a different strategy to break it into bits and process it.

  1.  public class BinaryToDecimal {
  2.  
  3.      public static void main(String[] args) {
  4.          int bin1=1110110,bin2=11110,bin3=1011;
  5.          System.out.println(bin1+" = "+toDecimal3(bin1));
  6.          System.out.println(bin3+" = "+toDecimal3(bin2));
  7.          System.out.println(bin3+" = "+toDecimal3(bin3));
  8.      }
  9.  
  10.     public static int toDecimal3(int binary){
  11.         int decimal=0;
  12.         int counter=0;
  13.         while(binary>0){
  14.             int bit=binary%10;
  15.             int power=counter++;
  16.             binary/=10;
  17.             decimal+=bit * Math.pow(2, power);
  18.         }
  19.         return decimal;
  20.     }
  21.  
  22.  }


Output


1110110 = 118
1011 = 30
1011 = 11

Explanation

  • We will use a loop to process each bit of the number. To get the bit, take the remainder of the number by 10.
  • To keep track of which bit we are on, we have used a counter variable.
  • Divide the binary number by 10 to eliminate the last digit.
  • Same as the previous method, we have used the variable decimal to store the result. 
  • The loop will run until all the digits of the binary number are eliminated