Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Binary to Decimal Conversion using Java

Last Updated on May 6, 2024 by Abhishek Sharma

Binary to decimal conversion is a common operation in computer programming, especially when dealing with binary data or bitwise operations. In Java, you can easily convert a binary number to its decimal equivalent using the Integer.parseInt() method or by manually calculating the decimal value from the binary digits. This article provides a comprehensive guide on how to perform binary to decimal conversion in Java, along with some best practices and examples. An illustration of converting from binary to decimal is shown in the picture below. 10101 is a binary number used to explain mathematical calculations.

We need to follow the 3 simple steps. Always pick the digit from the right side of a binary integer, multiply it by the power of 2, and then simply add them. The result will be the decimal number 21, in this situation.

## Ways to Convert Binary to Decimal using Java

We can convert binary to a decimal using custom logic

### Using Custom Logic

Converting a binary number to its decimal equivalent using custom logic is a simple process that involves the use of basic arithmetic operations and looping constructs.

Code Implementation

class BinaryToDecimal {
public static void main(String[] args) {
int binaryNumber = 10101;
int decimalNumber = 0;
int base = 1;
int temp = binaryNumber;
while (temp > 0) {
int lastDigit = temp % 10;
temp = temp / 10;
decimalNumber += lastDigit * base;
base = base * 2;
}
System.out.println("Binary number: " + binaryNumber);
System.out.println("Decimal number: " + decimalNumber);
}
}

Output

Binary number: 10101
Decimal number: 21

In the code above, we have defined a binary number as an integer binary number with the value of 10101. We then initialize a decimal number variable to 0, a base variable to 1, and a temporary variable temp to the value of the binary number.

We then loop through each digit of the binary number from right to left by using the modulo operator and integer division to extract the last digit, and then divide the temporary variable by 10 to remove the last digit. We then add the product of the last digit and the base value to the decimal number and multiply the base value by 2 for the next iteration of the loop.

Finally, we print both the binary and decimal numbers using the System.out.println() method.

Note that this custom logic works for binary numbers represented as integers. If the binary number is represented as a string, you will need to convert it to an integer first before using this logic.

### Using Prebuilt Method

Converting a binary number to its decimal equivalent in Java is straightforward, as the language provides built-in methods for this purpose. You can use the parseInt() method of the Integer class to convert a binary number represented as a string to its decimal equivalent.

Code Implementation

class BinaryToDecimal {
public static void main(String[] args) {
int binaryNumber = 10101;
int decimalNumber = 0;
int base = 1;
int temp = binaryNumber;
while (temp > 0) {
int lastDigit = temp % 10;
temp = temp / 10;
decimalNumber += lastDigit * base;
base = base * 2;
}
System.out.println("Binary number: " + binaryNumber);
System.out.println("Decimal number: " + decimalNumber);
}
}

Output

Binary number: 10101
Decimal number: 21

In the code above, we have defined a binary number as a string binaryNumber with the value of "10101". We then use the parseInt() method to convert this binary number to its decimal equivalent by specifying the radix value as 2. The parseInt() method returns an integer value that represents the decimal equivalent of the binary number.

Note that the parseInt() method throws a NumberFormatException if the input string is not a valid binary number. Also, this method works for binary numbers represented as strings. If the binary number is represented as an integer, you can simply use the decimal value directly without any conversion.

Conclusion
Converting a binary number to its decimal equivalent is a fundamental operation in computer programming. In Java, you can easily perform this conversion using the Integer.parseInt() method or by manually calculating the decimal value. Understanding how to convert between different number systems is essential for working with binary data and bitwise operations in Java.

In general, the choice of method for converting binary to decimal in Java will depend on the specific requirements of the application, including the size and complexity of the binary numbers to be converted, the desired level of precision, and the overall performance goals of the application.

## Frequently Asked Questions Related to Binary to Decimal Conversion using Java

Here are some of the FAQs related to Binary to Decimal Conversion using Java:

1. Can I convert a binary number with leading zeros to decimal in Java?
Yes, you can convert a binary number with leading zeros to decimal in Java using the Integer.parseInt() method with a radix of 2. For example, Integer.parseInt("0010", 2) will return 2.

2. How do I convert a decimal number to binary in Java?
You can convert a decimal number to binary in Java using the Integer.toBinaryString() method. For example, Integer.toBinaryString(10) will return "1010".

3. What is the maximum size of a binary number that can be converted to decimal in Java?
The maximum size of a binary number that can be converted to decimal in Java depends on the maximum value of an integer, which is Integer.MAX_VALUE (2,147,483,647). Any binary number larger than this value will result in an overflow error.

4. Are there any libraries in Java for working with binary numbers?
Yes, Java provides the java.lang.Integer class for working with binary numbers. This class includes methods for converting binary numbers to decimal and vice versa, as well as performing bitwise operations.

5. Is it possible to convert binary numbers to decimals using other number systems, such as hexadecimal or octal?
Yes, you can use the same parseInt() method of the Integer class to convert binary numbers to decimal using other number systems by specifying the appropriate radix value (16 for hexadecimal and 8 for octal) as the second argument to the method.