Binary to Decimal in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Introduction

Binary numbers are the numbers in which we use only two digits, 0s and 1s, to encode the information in a computer system. These numbers are the core of every computational device. Binary Numbers are represented by the base2 number system.

For example,
(101)2 in Binary = 5 in Decimal, (110)2 in Binary = 6 in Decimal etc.

binary representation

We'll see the manual conversion of Binary Numbers to Decimal Numbers in the next section. Now, let's discuss Decimal Numbers.

Decimal numbers are the numbers in which we use a range of digits from 0-9. These numbers are usually involved in human calculations. Decimal numbers are represented by the base10 number system.

For example: 1023456789,153,71,5121023456789, 153, 71, 512 etc.

decimal represention

Fun Fact: If the last digit of a Binary Number is 11 then its decimal form will always be an Odd Number, and if the last digit is 00 then its decimal form will always be an Even Number.

Convert Binary Number to Decimal Number

It is very easy to convert binary numbers to decimal numbers. We just have to take each binary digit from the right side of the number and multiply them by their place values according to the base2 number system, i.e. consecutive powers of 2 (starting from 0). And add all the product values to get a decimal number equivalent to the respective binary number. Let us look at an example to understand the conversion better:

We have taken a binary number 10111 and multiplied each digit from the right to the consecutive powers of 2 to get the decimal equivalent of 39.

binary conversion

Now, let us look at how to convert Binary Numbers to Decimal Numbers in a Java program using the custom method.

Method 1: Convert Binary to Decimal in Java Using Custom Method

We will take a very similar approach as discussed above to convert our Binary Numbers to Decimal Numbers using the Java program. We are just extracting each binary digit from the right side of the number by dividing it by 10 until the binary number becomes 0. After that we multiply the extracted digit to the consecutive powers of 2 (starting from 0) and add all the product values to get an equivalent decimal number of the respective binary number. Let us now look at the Java program:

Java Program:

Check and run this program using InterviewBit IDE.

Custom Input:

Output:

Explanation:

  • We are taking a long input from the user and storing it in a binaryNumber variable.
  • We have stored the return value of BinaryToDecimal() method into decimalNumber method, which converts the binary number to a decimal number and returns a converted decimal equivalent value of type long.
  • BinaryToDecimal() function uses the logic discussed above.
  • At last, we have printed the value of the binary number (100111) and equivalent decimal number (39).

Now, let us look at Binary to Decimal conversion using the Integer.parseInt() method in Java.

Method 2: Convert Binary to Decimal in Java using Integer.parseInt() Method

The Integer is a wrapper class that defines the parseInt() method in Java. In the program below, we are using the Integer.parseInt(String s, int radix) type of parseInt() method in which the String argument is parsed as a signed decimal integer object according to the radix argument passed in the method. We are passing a binary number of type String with a radix value of 2 (as binary follows base2 number system) in the Integer.parseInt() method to get a decimal equivalent of the binary number. The characters in the passed String should only be in the digits form. Let us now look at the Java program:

Java Program:

Check and run this program using InterviewBit IDE.

Custom Input:

Output:

Explanation:

  • We are taking a String input from the user and storing it in a binaryNumber variable.
  • We have stored the return value of the Integer.parseInt() method into decimalNumber method, which converts the binary number to a decimal number and returns a converted decimal equivalent value of type int.
  • At last, we have printed the value of the binary number (11110) and equivalent decimal number (30).

Convert Decimal Number to Binary Number

To convert decimal numbers to binary numbers, we just use an inverse technique that we used to convert binary to decimal. Take the decimal number and consecutively divide the decimal number by 2 till we get 1, simultaneously storing the remainder in a variable with a multiply of 10 at each step. At last, print the variable that stored the remainder. Let us look at an example to understand the conversion better:

We have taken a decimal number 39 and divided it consecutively by 2 and stored the remainder in the right to get the binary equivalent of 100111.

decimal conversion to binary

Now, let us look at how to convert Decimal Numbers to Binary Numbers in a Java program using a custom method.

Method 1: Convert Decimal to Binary in Java using Custom Method

We are going to take a similar approach as discussed above to convert our Decimal Numbers to Binary Numbers using the Java program. We are consecutively dividing the decimal number by 2 till we get 1 and simultaneously storing the remainder in a variable with a multiply of 10 at each step, then printing the variable that stored the remainders, i.e., the binary equivalent of the decimal number. Let us now look at the Java program:

Java Program:

Check and run this program using InterviewBit IDE.

Custom Input:

Output:

Explanation:

  • We are taking a long input from the user and storing it in a decimalNumber variable.
  • We have stored the return value of DecimalToBinary() method into binaryNumber method which converts the decimal number to a binary number and returns the converted binary equivalent value of type long.
  • At last, we have printed the value of the decimal number (39) and binary equivalent number (100111).

Now, let us look at Decimal to Binary Conversion using toBinaryString() method in Java.

Method 2: Convert Decimal to Binary in Java Using the toBinaryString() method

In the program below, we are using the toBinaryString() method defined in the Integer wrapper class. The toBinaryString() method takes an integer argument and returns a string representation of the integer argument as an unsigned integer in binary base2. We have passed an int decimal number in the toBinaryString() method, which returns the binary equivalent value in type String. Let us now look at the Java program:

Java Program:

Check and run this program using InterviewBit IDE.

Custom Input:

Output:

Explanation:

  • We are taking an int input from the user and storing it in a decimalNumber variable.
  • We have stored the return value of Integer.toBinaryString() method into binaryNumber method which converts the decimal number to binary number and returns a converted binary equivalent value of type String.
  • At last, we have printed the value of the decimal number (3030) and binary equivalent number (1111011110).

Conclusion

  • Binary numbers are used for encoding computer information in 0s and 1s, while Decimal numbers are generally used for human calculations using numbers in the range of 0-9.
  • Integer.parseInt() method takes a binary value of type String and returns a converted decimal equivalent value of type int.
  • Integer.toBinaryString() method takes a decimal value of type int and returns a converted binary equivalent value of type String.
  • In the article, we discussed the conversion of binary to decimal and vice versa using built-in and custom methods as well.