Java Convert Binary to Decimal

Java's binary to decimal conversion process entails changing a number's binary representation (base 2) into its decimal representation (base 10). Using the Integer.parseInt() function, which translates a binary text to its decimal equivalent, is one popular technique.

Integer.parseInt("1010", 2), for instance, would provide 10. Using custom logic is an alternative method that involves iterating through each binary number digit, beginning with the least significant digit, multiplying it by 2 raised to the power of its location, and then adding together all the products to obtain the decimal value. Greater flexibility and comprehension of the conversion process are made possible by this method.

Java Binary to Decimal conversion: Integer.parseInt()

The Integer.parseInt() method converts string to int with given redix. The signature of parseInt() method is given below:

Let's see the simple example of converting binary to decimal in Java.

Filename: BinaryToDecimalExample1.java

Output:

10

Explanation

This Java code snippet converts a binary string into its decimal equivalent using the Integer.parseInt() method. Initially, the binary string "1010" is assigned to the variable binaryString. Then, Integer.parseInt(binaryString, 2) is employed to convert this binary string to its decimal representation, utilizing a radix of 2 to denote the binary base. Finally, the resulting decimal value is printed to the console. Thus, executing this code outputs 10, representing the decimal equivalent of the binary number "1010".

Let's see another example of Integer.parseInt() method.

Filename: BinaryToDecimalExample2.java

Output:

10
21
31

Explanation

This sample of Java code shows how to use the Integer.parseInt() method to convert binary strings to their decimal equivalents. Three binary strings, "1010", "10101", and "11111", are converted to decimal numbers within the main procedure. To indicate the binary base, the radix is set to 2, and each binary string is supplied as the first input to Integer.parseInt(). The decimal numbers obtained are displayed on the terminal. This code, when run, produces the decimal representations of the binary values "1010", "10101", and "11111".

Java Binary to Decimal conversion: Using Custom Logic

Another method involves custom logic, where you iterate over each digit of the binary number, starting from the least significant digit, and multiply each digit by 2, raising to the power of its position, then summing up all the products to get the decimal value.

Filename: BinaryToDecimalExample3.java

Output:

Decimal of 1010 is: 10
Decimal of 10101 is: 21
Decimal of 11111 is: 31

Explanation

This Java code shows how to use custom logic to convert binary numbers to their decimal equivalents. A while loop is used in the getDecimal method to traverse over each binary integer digit. The decimal value of each digit is calculated by multiplying it by two raised to the power of its location. The final decimal equivalent is then calculated by adding these decimal numbers. This procedure is demonstrated in the code for the binary integers 1010, 10101, and 11111, which, when run, result in their corresponding decimal representations.

Java Binary to Decimal conversion: Using BigInteger()

BigInteger class in Java provides a convenient way to handle large binary numbers and convert them to decimal.

BigInteger(String val, int radix) Constructor: This constructor creates a BigInteger object from a string representation of a number. The first argument val is the string representation of the number, and the second argument radix specifies the base of the number system used in the string. For binary to decimal conversion, the radix should be set to 2.

Filename : BinaryToDecimalExample4.java

Output:

Decimal of 1010 is: 10
Decimal of 10101 is: 21
Decimal of 11111 is: 31

Explanation

In this example, the BigInteger constructor is used to convert three binary strings (1010, 10101, and 11111) to their corresponding decimal values. The input strings are in binary format, as indicated by the 2 passed as the second argument. The decimal equivalents are then displayed on the console.






Latest Courses