Java Binary to Decimal Conversion with Examples
Introduction
In Java programming, converting a binary number into a decimal number is a common mathematical and programming task. Binary numbers use the base-2 number system, which contains only two digits: 0 and 1. Decimal numbers use the base-10 number system, which contains digits from 0 to 9.
Java provides several ways to convert binary numbers into decimal numbers. We can use built-in methods such as Integer.parseInt() as well as mathematical calculations and loops to perform this conversion.
In this article, we will learn how to convert binary to decimal in Java with different methods and practical examples.
What Is a Binary Number?
A binary number is a number represented using the base-2 number system. It consists of only two digits, 0 and 1.
Computers use the binary number system internally to represent and process information.
Examples of binary numbers:
- 1010
- 1100
- 1001
- 1111
- 10101
What Is a Decimal Number?
A decimal number is a number represented using the base-10 number system. It uses ten digits, from 0 to 9.
Decimal numbers are commonly used in everyday calculations.
Examples of decimal numbers:
- 10
- 12
- 15
- 21
- 25
How to Convert Binary to Decimal?
To convert a binary number into a decimal number, multiply each binary digit by the corresponding power of 2, starting from the rightmost digit with power 0.
For example, let’s convert the binary number 1010 into decimal.
Calculation:
1010 = (1 × ²³) + (0 × ²²) + (1 × ²¹) + (0 × ²⁰)
= 8 + 0 + 2 + 0
= 10
Therefore, the decimal equivalent of the binary number 1010 is 10.
1. Using Integer.parseInt() Method
Java provides the Integer.parseInt() method to convert a binary string into its decimal integer equivalent.
The method accepts two arguments:
- The binary number as a string.
- The base (radix) of the number system, which is 2 for binary.
Example:
public class BinaryToDecimal {
public static void main(String[] args) {
String binary = "1010";int decimal = Integer.parseInt(binary, 2); System.out.println("Decimal number: " + decimal);
}
}
Output:
Decimal number: 10Explanation:
- We declare a string variable named
binaryand initialize it with the value "1010". - The
Integer.parseInt(binary, 2)method converts the binary string into a decimal integer. - The converted result is stored in the
decimalvariable. - Finally, the
System.out.println()method displays the decimal value.
2. Using a While Loop
We can also convert a binary number into a decimal number using a while loop. In this approach, we extract each digit from the right side and multiply it by the corresponding power of 2.
Get Rachitalmora’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
public class BinaryToDecimal {
public static void main(String[] args) {
int binary = 1010;
int decimal = 0;
int base = 1;while (binary > 0) {
int remainder = binary % 10; decimal += remainder * base; binary = binary / 10;
base = base * 2;
} System.out.println("Decimal number: " + decimal);
}
}
Output:
Decimal number: 10Explanation:
- The
binaryvariable stores the binary number 1010. - The
decimalvariable is initialized to 0. - The
basevariable starts with 1, representing 2⁰. - The modulo operator (%) extracts the rightmost binary digit.
- The extracted digit is multiplied by the current base and added to the decimal value.
- The binary number is divided by 10 to remove its last digit.
- The base is multiplied by 2 in every iteration.
- The loop continues until all binary digits have been processed.
3. Using a For Loop
Another approach is to use a for loop to convert a binary number into its decimal equivalent. This method processes each digit from right to left.
Example:
public class BinaryToDecimal {
public static void main(String[] args) {
String binary = "10101";
int decimal = 0;
int power = 0;for (int i = binary.length() - 1; i >= 0; i--) {
int digit = binary.charAt(i) - '0'; decimal += digit * Math.pow(2, power); power++;
} System.out.println("Decimal number: " + decimal);
}
}
Output:
Decimal number: 21Explanation:
- We initialize a string variable with the binary value “10101”.
- The for loop starts from the last character of the string.
- The
charAt()method retrieves each binary digit. - Each digit is multiplied by the corresponding power of 2.
- The calculated value is added to the decimal variable.
- The loop continues until all digits have been processed.
4. Using the BigInteger Class
Java also provides the BigInteger class to handle large binary numbers that may exceed the range of the primitive integer data types.
The BigInteger class provides the BigInteger(String val, int radix) constructor, which can convert a binary string into a decimal number.
Example:
import java.math.BigInteger;public class BinaryToDecimal {
public static void main(String[] args) {
String binary = "1010101010101010"; BigInteger decimal = new BigInteger(binary, 2); System.out.println("Decimal number: " + decimal);
}
}
Output:
Decimal number: 43690Explanation:
- We import the
java.math.BigIntegerpackage. - The binary number is stored in a string variable.
- The
BigIntegerconstructor accepts the binary string and radix 2. - It converts the binary value into a decimal representation.
- Finally, the result is displayed using the
System.out.println()method.
Advantages of Binary to Decimal Conversion in Java
- Data Representation: Helps understand how computers represent numerical information.
- Programming Practice: Improves understanding of loops, operators, and mathematical calculations.
- Number System Knowledge: Develops knowledge of different number systems.
- Problem Solving: Helps programmers solve number conversion problems.
- Large Number Support: The BigInteger class allows conversion of binary values beyond the range of standard integer types.
Conclusion
Converting binary numbers into decimal numbers in Java is a useful programming concept. Java provides multiple approaches for performing this conversion, including Integer.parseInt(),while loops, for loops, and the BigInteger class.
The Integer.parseInt() method is a simple, convenient way to convert binary strings to integers. For larger binary values, the BigInteger class is a suitable choice.
Understanding these methods helps beginners improve their Java programming skills and better understand number systems.








