×

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

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

How to create a linked list in Java

Introduction: The linked listing is one type of linear statistics shaped like an array. Not like arrays, linked listing factors aren't stored in a contiguous place. The elements have linked...

3 minutes read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

Best Practices to use String Class in Java

Use String Builder or String Buffer for String concatenation in place of + operator.Compare two strings by equals( ) method instead == operator.Call .equals( ) method on a known String...

3 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

Heap Implementation in Java

The root node or parent node of a Java heap is compared to its left and right offspring, and the children are then ordered in line with the comparison.Assuming that...

9 minutes read.

Difference Between BufferedReader and FileReader

BufferedReader: To read data from a specified character stream, two classes are used: buffered readers and file readers. Both of them have advantages and disadvantages. Although how they operate is the...

6 minutes read.

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Character Array in Java

A character array is an array which holds values of character data types. It is different from a string array. The character array, string, and StringBuffer classes in the Java...

4 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.