Convert Binary To Decimal How To: In this blog post, we will discuss converting a binary number system into a decimal number system. Binary numbers are the core of each and every computational device. The base2 number system represents them and uses only two digits: 0s and 1s.
For a Better understanding of these programs, you must have some programming knowledge on Java’s few topics like:
- Java Methods
- Java Operators
- Java while Loop
Post Type: | Java Programs For Beginners |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | Join Our Telegram Group |
Convert Binary Number to Decimal Number
To convert a binary to decimal, start from the right side of the number and multiply each digit by its place value. The place values in binary go up in powers of 2, beginning at 0. Add all the products together to get the equivalent decimal number.
We can convert Binary To Decimal in the following 2 ways that are:
- Using the Integer.parseInt() method for the Integer class,
- By using the custom method
Binary To Decimal How To Use Integer.parseInt() Method
The Integer class is a Java wrapper class with a parseInt() method, which can convert a String into an int. The program below uses the parseInt() method with two arguments: a String and an int. The first argument is parsed as a signed decimal integer according to the second argument’s value (the radix).
We are converting a binary number to its decimal equivalent using the Integer.parseInt () method. We pass in the binary number as a string with a radix value of 2, as binary numbers follow the base2 numbering system.
Note: The characters in the passed String should only be in the digits form.
package com.softwaretestingo.basic; import java.util.Scanner; public class BinarytoDecimalEx1 { public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println("Enter a Binary Number : "); // Enter a binary number in string form String binaryNumber = scn.nextLine(); // passing binary number and base in Integer.parseInt() method // converting binary to decimal, return type int int decimalNumber = Integer.parseInt(binaryNumber, 2); // printing the values of binary and decimal equivalent System.out.println("Binary Number : " + binaryNumber); System.out.println("Decimal Number Equivalent : " + decimalNumber); } }
Output:
Enter a Binary Number : 10010 Binary Number : 10010 Decimal Number Equivalent : 18
Binary To Decimal How To Use Custom Method
We can convert a binary number to its decimal equivalent by extracting each binary digit from the right side of the number and dividing it by 10 until the binary number becomes 0. For each extracted digit, we multiply it by consecutive powers of 2 (starting from 0) and add all product values together to get an equivalent decimal number of the respective binary number.
package com.softwaretestingo.basic; import java.util.Scanner; public class BinarytoDecimalEx2 { public static void main(String[] args) { Scanner scn = new Scanner(System.in); System.out.println("Enter a Binary Number : "); // input a binary number long binaryNumber = scn.nextLong(); // calling the method BinaryToDecimal and storing it in decimalNumber long decimalNumber = BinaryToDecimal(binaryNumber); // printing the values of binary and decimal equivalent System.out.println("Binary Number : " + binaryNumber); System.out.println("Decimal Number Equivalent : " + decimalNumber); } public static long BinaryToDecimal(long binaryNum) { long decimalEquivalent = 0; long lastDigit = 0; int power = 0; // binary to decimal conversion while(binaryNum > 0) { // getting the right most digit after every division by 10 lastDigit = binaryNum % 10; // adding the product of lastDigit and 2^power decimalEquivalent += lastDigit * Math.pow(2, power); power++; binaryNum /= 10; } return decimalEquivalent; } }
Output:
Enter a Binary Number : 10010 Binary Number : 10010 Decimal Number Equivalent : 18
Conclusion:
Finally, we have reached the end of the Convert Binary To Decimal Java Program. You can try your own, and if you find any difficulty, let us know in the comment section, and we will try to help you.