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.
- public class BinaryToDecimal {
- public static void main(String[] args) {
- String bin1=“1110110”,bin2=“11110”,bin3=“1011”;
- System.out.println(bin1+” = “+toDecimal1(bin1));
- System.out.println(bin3+” = “+toDecimal1(bin2));
- System.out.println(bin3+” = “+toDecimal1(bin3));
- }
- public static int toDecimal1(String binary){
- return Integer.parseInt(binary,2);
- }
- }
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
- public class BinaryToDecimal {
- public static void main(String[] args) {
- String bin1=“1110110”,bin2=“11110”,bin3=“1011”;
- System.out.println(bin1+” = “+toDecimal2(bin1));
- System.out.println(bin3+” = “+toDecimal2(bin2));
- System.out.println(bin3+” = “+toDecimal2(bin3));
- }
- public static int toDecimal2(String binary){
- char[] c=binary.toCharArray();
- int len=c.length;
- int decimal=0;
- for (int i=0;i<len;i++){
- int bit=c[i]–‘0’;
- int power=len–i–1;
- decimal+=bit * Math.pow(2, power);
- }
- return decimal;
- }
- }
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.
- public class BinaryToDecimal {
- public static void main(String[] args) {
- int bin1=1110110,bin2=11110,bin3=1011;
- System.out.println(bin1+” = “+toDecimal3(bin1));
- System.out.println(bin3+” = “+toDecimal3(bin2));
- System.out.println(bin3+” = “+toDecimal3(bin3));
- }
- public static int toDecimal3(int binary){
- int decimal=0;
- int counter=0;
- while(binary>0){
- int bit=binary%10;
- int power=counter++;
- binary/=10;
- decimal+=bit * Math.pow(2, power);
- }
- return decimal;
- }
- }
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