Binary to Decimal in programming languages

You can find here how to convert from Binary to Decimal in C++, Java, Python, Php and so on. If you want to convert from bin to dec, you can use our converter.

How to convert

Java

Function public static int parseInt(String s, int radix) throws NumberFormatException converts string value  s  to int value with the radix in Integer class [ref]

Bin to dec in Java

int decimalValue = Integer.parseInt("110010", 2);
Output: 50

PHP

Function bindec ( string $binary_string ) : numberreturns the decimal equivalent of the binary number represented by the binary_string argument [ref].

Bin to dec in PHP

<?php
echo bindec('111');
?>
Output: 7

Python 3

Function int(x, base=10)returns an integer object constructed from a number or string x with the base. [ref]

Bin to dec in Python 3

>>> int('11111111', 2)
Output: 255

 

Programs for converting bin to dec

<?php 
// PHP program to convert binary to decimal 
  
// Function to convert from bin to dec 
function binToDec($number) 
{ 
  $dec_value = 0; 
  $base = 1; 
      
  $temp = $number; 
  while ($temp) 
  { 
    $digit = $temp % 10; 
    $temp = $temp / 10; 
          
    $dec_value += $digit * $base; 
    $base = $base * 2; 
  } 
  return $dec_value; 
} 
  
echo binToDec(110110),"\n"; 
?> 

/* C++ program to convert bin to dec */
#include <iostream>
using namespace std;
int main()
{
  long bin, dec = 0, rem, num, base = 1;
  bin = 01011; 
  while (num > 0)
  {
    rem = num % 10;
    dec = dec + rem * base;
    base = base * 2;
    num = num / 10;
  }
  
  cout << "Result: " << dec << endl;
  return 0;
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>