How To Convert Binary To Decimal In Java
Tags: java, binarytodecimal, convertbinarytodecimal, algorithms
Published: 2020-05-22 18:51:58

<p>There are a multiple way to convert binary to decimal in Java:</p>
<h4><strong>Convert Binary To Decimal In <a href="https://onlinetutorials.tech/category/java-tutorials-online-tutorials/" rel="dofollow" target="_blank">Java</a> Interger.parseInt() method:</strong></h4>
<p>This is really one of the simplest way in <a href="https://onlinetutorials.tech/category/java-tutorials-online-tutorials/" rel="dofollow" target="_blank">java</a> to get Decimal number in <a href="https://onlinetutorials.tech/category/java-tutorials-online-tutorials/" rel="dofollow" target="_blank">java</a>:</p>
<blockquote>
<pre>package com.onlinetutorials.tech;
public class ConvertBinaryToDecimalInJava {
       public static void main(String args[]){
              String binarynumber="10101";
              int decimalnumber=Integer.parseInt(binarynumber,2);
              System.out.println(decimalnumber);
       }
}</pre>
</blockquote>
<p><strong>Output of above program after executions would be: 21.</strong></p>
<p>Example:</p>
<blockquote>
<pre>package com.onlinetutorials.tech;
public class ConvertBinaryToDecimalInJava {
       public static void main(String args[]){
            System.out.println(Integer.parseInt("1110",2));
            System.out.println(Integer.parseInt("0010",2));
            System.out.println(Integer.parseInt("1010",2));
            System.out.println(Integer.parseInt("0110",2));
            System.out.println(Integer.parseInt("1101",2));
       }
}</pre>
</blockquote>
<p><strong>Output of above program after executions would be:<br/>
14<br/>
2<br/>
10<br/>
6<br/>
13<br/>
</strong></p>
<h2><strong>Java program for </strong><strong>Binary To Decimal using </strong><strong>custom logic:</strong></h2>
<blockquote>
<pre>package com.onlinetutorials.tech;
public class ConvertBinaryToDecimalInJava {
       public static int retrieveDecimal(int binarynumber){
            int decimalnumber = 0;
            int power = 0;
            while(true){
                  if (binarynumber == 0){
                      break;
                  }else{
                      int temp = binarynumber%10;
                      decimalnumber += temp*Math.pow(2, power);
                      binarynumber = binarynumber/10;
                      power++;
                   }
            }
            return decimalnumber;
       }
       public static void main(String args[]){
            System.out.println("Decimal value is: "+retrieveDecimal(1110));
            System.out.println("Decimal value is: "+retrieveDecimal(0010));
            System.out.println("Decimal value is: "+retrieveDecimal(1010));
            System.out.println("Decimal value is: "+retrieveDecimal(0110));
            System.out.println("Decimal value is: "+retrieveDecimal(1101));
       }
}
</pre>
</blockquote>
<p><strong>Output of above program after executions would be:<br/>
14<br/>
8<br/>
10<br/>
16<br/>
13</strong></p>
You can visit <a href="https://onlinetutorials.tech/category/java-tutorials-online-tutorials/" rel="dofollow" target="_blank">onlinetutorials.tech</a> for more <a href="https://onlinetutorials.tech/category/java-tutorials-online-tutorials/" rel="dofollow" target="_blank">Java Tutorials</a>
Visit us for more tutorials:
<a href="https://onlinetutorials.tech/category/java-tutorials-online-tutorials/" target="_blank" rel="dofollow">Java Tutorial</a> 
<a href="https://onlinetutorials.tech/category/python-tutorials-from-onlinetutorials-tech/" target="_blank" rel="dofollow">Python Tutorial</a> 
<a href="https://onlinetutorials.tech/category/rabbitmq-tutorial/" target="_blank" rel="dofollow">RabbitMQ Tutorial</a> 
<a href="https://onlinetutorials.tech/category/spring-boot/" target="_blank" rel="dofollow">Spring boot Tutorial</a>