Overview
A binary number is a number which is composed of digits 1 and 0 such as 110011. A decimal number is a number which is composed of digits 0 to 9 such as 130672.
It is a common requirement in computer programming to convert a binary number to decimal number and vice-versa.
In this post, we will focus on different ways in which a binary number can be converted to decimal number in java.

Logic
To convert a binary number to decimal form, its digits are multiplied by 2 raised to the power of their position in the number and the result is added.
Final sum after multiplying the digits and adding them is the equivalent decimal of the binary number.
The position of digits is calculated starting from right to left beginning with 0. Thus first digit at the right is at position 0, second digit at position 1 and so on.
For Example, if the binary number is 110011, then according to the above logic, its decimal conversion will be done as : 1 * 20 + 1 * 21 + 0 * 22 + 0 * 23 + 1 * 24 + 1 * 25 = 1 + 2 + 0 + 0 + 16 + 32 = 51

Every number belongs to a number system. Each number system has its own base or radix.
Thus every number has a base which tells us about the number system of this number.
Following are the common number systems and their base (or radix) :

Binary Number System                      2
Octal Number System                        8
Decimal Number System                   10
Hexadecimal Number System           16

Method 1 : Using repeated division
This is the implementation of above mentioned algorithm.
Read a number from the user. Iterate over the digits of the number and in each iteration multiply the digit with 2 raised to the power its position in the number.
The product of all digits when added give the decimal equivalent of the binary number.
Iteration of the digits should start from right to left and hence in each iteration, the number is operated with % (modulus operator) by 10.
% operator gives the remainder of the division which will be the rightmost digit. This digit is then multiplied by 2 raised to the power of its position by using pow() method of java.lang.Math class.
The position of digit and the sum are maintained by variables indexand sum respectively.

static void methodTwo() {
  Scanner scanner = new Scanner(System.in);
  System.out.println("Enter the number");
  // declare string containing binary number
  long binaryNumber = scanner.nextLong();
  //represents the position of digits
  int index = 0;
  int sum = 0;
  while (binaryNumber > 0) {
    int rem = (int) binaryNumber % 10;
    binaryNumber = binaryNumber / 10;
    // multiply the digit by 2 raised to the power of its position
    // in the number
    int multiplier = (int) Math.pow(2, index);
    //add it to the previous sum of such products
    sum += rem * multiplier;
    radix++;
  }
  System.out.println("Decimal number is : " + sum);
  scanner.close();
}

Output  :

Enter the number
110011
Binary number converted to decimal number
Decimal number is : 51

Method 2 : Using Integer wrapper class
This is the simplest method of converting a binary number into decimal using an overloaded version of parseInt method in java.lang.Integer class which takes a String and a base (or radix)  in which it should parse the String as arguments, that is, it identifies the number system of the string, and returns its decimal equivalent.
Read the number as input using Scanner class object. Since the number entered is in binary format, pass 2 as radix and it will return the decimal equivalent of the binary number.

static void methodOne() {
  Scanner scanner = new Scanner(System.in);
  System.out.println("Enter the number");
  // declare string containing binary number
  String strBinaryNumber = scanner.nextLine();
  //Pass 2 as the radix since number is in binary format
  int decimalNumber = Integer.parseInt(strBinaryNumber, 2);
  System.out.println("Binary number converted to decimal number");
  System.out.println("Decimal number is : " + decimalNumber);
  scanner.close();
}

Output :

Enter the number
110011
Binary number converted to decimal number
Decimal number is : 51

Method 3 : Using Character wrapper class
This method is also based on the similar logic. The only difference between the implementation is that the binary number is read as a String and converted to a char array using the toCharArray()method of java.lang.String class.
This character array is then iterated and the characters are converted to their integer equivalent values and multiplied by 2 raised to the power of their index in the array.
The index in array will be the same as the position in number – 1. That is, element at 4th position will be at 3rd index in the array.
To convert a character to its integer value, static getNumeric()method of java.lang.Character wrapper class can be used.
The product of digits with 2 raised to the power of its position when added, yield the decimal equivalent of the binary number.

static void methodThree() {
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter the number");
   // declare string containing binary number
   String binaryNumber = scanner.nextLine();
   char[] digits = binaryNumber.toCharArray();
   int sum = 0;
   int index = 0;
   //reverse iterate the array since calculation is done from right to left
   for (int radix = digits.length - 1; radix >= 0; radix--) {
     char c = digits[index];
     index++;
     int numericValue = Character.getNumericValue(c);
     int multiplier = (int) Math.pow(2, radix);
     sum += numericValue * multiplier;
}
System.out.println("Decimal number is : " + sum);
scanner.close();
}

Output :

Enter the number
110011
Binary number converted to decimal number
Decimal number is : 51

Let’s tweak in :

  1. Method 1 uses a built in method of java api, hence when conversion is required using inbuilt java functionality, then this method should be used.
  2. If a negative binary number is passed to parseInt()method then the corresponding result is also a negative number.
    Example, conversion of binary -1101 will be -12.
  3. From the above point, it becomes clear that Method 1 has the support for negative number conversion, while Methods 2 and 3 need to be modified for this.
  4. If the String supplied to parseInt() method is not in the format of supplied radix, then a java.lang.NumberFormatExceptionis thrown such as passing String “543” with radix 2.
Hope you liked the different methods. There are more methods too. In case you know one, do let us know !!!

1

Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.