Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconSoftware Developmentbreadcumb forward arrow iconJava Program to Convert Binary to Decimal

Java Program to Convert Binary to Decimal

Last updated:
18th Feb, 2023
Views
Read Time
5 Mins
share image icon
In this article
Chevron in toc
View All
Java Program to Convert Binary to Decimal

The Binary number system is a base 2 system that consists of 0’s and 1’s. The binary numbers are well understood by computers and digital systems, hence termed as ‘machine language’. Each digit in a binary system holds the place value of the power of 2.

The decimal number system is identified by humans. It is a Base 10 numbering system, using digits from 0 to 9. Each digit in the decimal holds a place value of power of 10.

Let’s discuss how to convert binary to decimal in Java in this article. 

Importance of Binary to Decimal Conversion

The complex binary language needs conversion to a human-understandable format before it reaches the common users. Hence, we require calculation methods on how to convert binary to decimal and easily figure out what they signify.

Ads of upGrad blog

Check out our free technology courses to get an edge over the competition.

Mathematical calculation for converting binary to Decimal

Since binary numbers are base 2 numbering systems, every place holds the value 2n. For instance, the first place has a value 20 = 1, the second place has a value 21 = 2, the third place 22 = 4 and so on. 

Let’s consider an example:

Convert Binary number 101101 to decimal number:

101101

MSB                                                                                                          LSB

The number on the leftmost side is called MSB or Most Significant bit with a maximum power of 2, and the number on the Rightmost side is called LSB or Least Significant bit, with minimum power of 2.

Now, let’s see the mathematical binary decimal converter method for the following.

(101101)2

=1 x 25 + 0 x 24 + 1x 23 + 1x 22 + 0 x 21 + 1 x 20

= 32 +0 + 8 + 4 + 0+ 1

= (45)10

It seems interesting for a small binary number. But it is a difficult task to be considered while the number is really long. In such situations, we can’t depend on manual calculations, and we require a binary decimal converter.

Check Out upGrad’s Software Development Courses

Java Program to convert Binary to Decimal

Coding has its own importance in the digital era. It helps to replace the manual burden of decoding the machine language into a human-readable form. It enables a quick and efficient way of converting any binary number into a decimal number with no tedious efforts.

Let’s learn how to convert binary to decimal in Java. There are two possible methods to write a Java program to convert binary to decimal.

  1. Using ParseInt () method of class Integer
  2. User defined or custom logic method

Explore our Popular Software Engineering Courses

  • Using ParseInt () Method

In the program, two arguments are generally used. The first argument is String and the second one is the base required to convert the number. The output will be an integer that is represented by the string argument in the mentioned base.

There are two possible ways of using ParseInt () to convert binary to decimal in Java.

  • By prompting the user to enter the binary number:

Input:

Output:

Reference image: URL_beginnersbook_2014/07

In the above program, we make use of the Scanner class. Usually, we use the Scanner class in case the user input is required in the program. The Scanner class is found in the Java.util package. 

In-Demand Software Development Skills

Hence, the user is asked to provide a binary number to be converted to decimal as per the choice, and the converted output is obtained for the number provided.

  • By including the binary number in the program itself:

Input:

Output:

Reference Image: Reference_Javatpoint

In the above program, the binary numbers are included and converted decimal output is obtained for the input binary numbers.

  • User Defined logic/ Custom Logic

Input:

Output:

Reference Image: Reference_Javatpoint

The above Java program doesn’t use the ParseInt function. The program is written with user defined inputs. 

Binary numbers are most preferred and understood by modern digital and electronic machines, whereas decimal numbers are perceived by the general audience. Hence, both are necessary in the current world. 

Ads of upGrad blog

Read our Popular Articles related to Software Development

The quick and effortless conversion of binary to decimal in Java saves the time and reduces the burden of calculating it manually.

At upGrad, we understand the importance of practical, hands-on learning – especially when it comes to software development. As a result, our courses and training initiatives have practicality at their very core. One such initiative is Full Stack Development Bootcamp which will help you develop all the relevant skills required to excel in full-stack development. 

Profile

Pavan Vadapalli

Blog Author
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology strategy.

Explore Free Courses

Suggested Tutorials

View All

Suggested Blogs

Top 14 Technical Courses to Get a Job in IT Field in India [2024]
90952
In this Article, you will learn about top 14 technical courses to get a job in IT. Software Development Data Science Machine Learning Blockchain Mana
Read More

by upGrad

15 Jul 2024

25 Best Django Project Ideas & Topics For Beginners [2024]
143863
What is a Django Project? Django projects with source code are a collection of configurations and files that help with the development of website app
Read More

by Kechit Goyal

11 Jul 2024

Must Read 50 OOPs Interview Questions & Answers For Freshers & Experienced [2024]
124781
Attending a programming interview and wondering what are all the OOP interview questions and discussions you will go through? Before attending an inte
Read More

by Rohan Vats

04 Jul 2024

Understanding Exception Hierarchy in Java Explained
16879
The term ‘Exception’ is short for “exceptional event.” In Java, an Exception is essentially an event that occurs during the ex
Read More

by Pavan Vadapalli

04 Jul 2024

33 Best Computer Science Project Ideas & Topics For Beginners [Latest 2024]
198249
Summary: In this article, you will learn 33 Interesting Computer Science Project Ideas & Topics For Beginners (2024). Best Computer Science Proje
Read More

by Pavan Vadapalli

03 Jul 2024

Loose Coupling vs Tight Coupling in Java: Difference Between Loose Coupling & Tight Coupling
65177
In this article, I aim to provide a profound understanding of coupling in Java, shedding light on its various types through real-world examples, inclu
Read More

by Rohan Vats

02 Jul 2024

Top 58 Coding Interview Questions & Answers 2024 [For Freshers & Experienced]
44555
In coding interviews, a solid understanding of fundamental data structures like arrays, binary trees, hash tables, and linked lists is crucial. Combin
Read More

by Sriram

26 Jun 2024

Top 10 Features & Characteristics of Cloud Computing in 2024
16289
Cloud computing has become very popular these days. Businesses are expanding worldwide as they heavily rely on data. Cloud computing is the only solut
Read More

by Pavan Vadapalli

24 Jun 2024

Top 10 Interesting Engineering Projects Ideas & Topics in 2024
43094
Greetings, fellow engineers! As someone deeply immersed in the world of innovation and problem-solving, I’m excited to share some captivating en
Read More

by Rohit Sharma

13 Jun 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon