×

How to Convert Binary to Decimal in Java

How to Convert Binary to Decimal in Java There are two methods to convert binary to decimal.
  • Using Integer.parseInt() method
  • Using user-defined logic

Using Integer.parseInt() method

The parseInt() is a static method of Integer wrapper class. It converts string to integer with specified radix or base. It takes two arguments. First for binary string, and second for radix (in which we have to convert the number). It returns the integer represented by the string argument in the specified radix. The signature of the method is given below: public static int parseInt(String str,int radix) Example In the following example, we have taken two variable d1 and d2 of type int, which stores the converted decimal value of the corresponding binary number. The Integer is a wrapper class. It invokes the parseInt(string binary_number,int radix) method of Integer class. It parses two arguments first one is the string of boolean number and the second one is the radix. It returns the corresponding decimal number. The first println statement prints the decimal value of 101101 and the second println statement prints the decimal value of 1001001.
public class BinaryToDecimalExample
{ 
public static void main(String args[])
{ 
int d1=Integer.parseInt("101101",2);
int d2=Integer.parseInt("1001001",2);
System.out.println("The decimal equivalent of the number is: "+d1); 
System.out.println("The decimal equivalent of the number is: "+d2);
}
}
Output
The decimal equivalent of the number is: 45
The decimal equivalent of the number is: 73

Using user-defined logic

In this method, we have to define our logic for converting binary to decimal. Example In the following example, we have taken a variable bnum (represent binary number) of type integer and initialize 1001 to it. We have defined two more int variables dnum (represent decimal number) and pwr (represent power) and initialize 0 to both. Let’s see how the body of the loop will execute? The body of the while loop has three statements. The first statement calculates the required decimal number. bnum%10 calculates the remainder. Math.pow(2,pwr) is the method of java.lang.Math class, which returns the value of the first argument raised to the power of the second argument. After getting both results, we have multiplied both values, and add and store the resultant to the dnum variable. The second statement updates the value of bnum, i.e., it divides the bnum by 10 and takes the quotient. The while loop will execute until the specified condition becomes false. The third statement increments by 1 in the pwr (power) variable. When the specified condition gives false, then the loop will terminate the execution. The println statement prints the equivalent decimal number, which is stored in dnum variable.
Public class BinaryToDecimalExample1
{
public static void main(String args[])
{
int bnum=1001;
int dnum=0,pwr=0;
while(bnum!=0)
{
dnum+=((bnum%10)*Math.pow(2,pwr));
bnum=bnum/10;
pwr++;
}
System.out.println("The decimal equivalent of given binary is: "+dnum);
}
}
Output
The decimal equivalent of given binary is: 9

Related Topics

Java Interface Keyword

An interface is also known as the blueprint in Java. It has constants of static values and methods of abstraction. The interface is a mechanism used by Java to declare...

3 minutes read.

Java Else Keyword

The else statement specifies a section of Java code that will run if an if statement's condition is false.The following conditional statements can be used in Java:To provide a block...

3 minutes read.

Java Integer toHexString() method

The toHexString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 16. Syntax public static String toHexString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

Methods in Java

The Methods in Java are the collection of statements that are executed when the method is called. By using the methods, the complexity of writing the code decreases. The method consists...

4 minutes read.

Prime Number Program in Java Using a Scanner

In Java, a prime number is one that can only be divided by one or by itself and is greater than one. In other words, only one or itself can...

3 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

3 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Order of Execution of Constructors in Java Inheritance

Constructor in Java There are several distinctions between a method and a function object() in Java. The name of the function object()  is identical to the class name. There is no...

4 minutes read.

Fibonacci Series Program in Java

Fibonacci Series Program in Java using Recursion Fibonacci series is a series whose every term is comprised of adding its previous two terms, barring the first two terms 0 and 1....

3 minutes read.

Graph Problems in Java

A graph is a special type of data structure used in programming that is made up of edges connecting each set of finitely many nodes, or vertices, to each other....

14 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Powerful Number in Java

We will define a powerful number in this article and write Java programs to determine whether a given number is a powerful number or not. Java coding interviews and academic...

6 minutes read.

Access specifiers in Java

What are access specifiers in Java? Access specifiers in Java are used to determine whether other classes can use a particular field or invoke a particular method. Access specifiers mainly specify...

7 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.