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 15, 2023 by Prepbytes

Binary to decimal conversion is a process of converting a number represented in binary form to its equivalent decimal representation.
Binary numbers are those with just the digits 0 and 1 in them. Only the base-2 number system is used to express them. We are aware that the natural digital language of contemporary computers is the binary system. But decimal numerals are more familiar to us. Of course, binary numbers may be converted to decimal numbers, and vice versa. 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 in Java is a simple process that can be accomplished using either custom logic or the built-in parseInt() method of the Integer class. The custom logic involves extracting each digit of the binary number and multiplying it with the corresponding power of 2 to obtain the decimal equivalent. On the other hand, the parseInt() method provides a convenient way to convert a binary number represented as a string to its decimal equivalent.

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

Q1. What is the maximum size of the binary number that can be converted to a decimal using the parseInt() method?
Ans. The maximum size of the binary number that can be converted to a decimal using the parseInt() method is limited by the maximum value of an integer in Java, which is 2,147,483,647. If the binary number exceeds this value, you will need to use a custom logic approach to handle the conversion.

Q2. What happens if the input string to the parseInt() method is not a valid binary number?
Ans. The parseInt() method throws a NumberFormatException if the input string is not a valid binary number. You can catch this exception and handle it appropriately in your code.

Q3. Can the parseInt() method be used to convert binary numbers represented as integers?
Ans. No, the parseInt() method can only be used to convert 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.

Q4. How accurate is the decimal equivalent obtained from the parseInt() method?
Ans. The decimal equivalent obtained from the parseInt() method is accurate for the binary number represented as a string. However, due to the limitations of floating-point arithmetic, the accuracy of the decimal equivalent may be affected for very large or very small binary numbers.

Q5. Is it possible to convert binary numbers to decimals using other number systems, such as hexadecimal or octal?
Ans. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *