How to use Conversion Algorithm In PHP
In this case, we use mathematical conversion algorithm to convert binary to its decimal equivalent. Here, we convert a binary number to decimal by iterating through each bit and summing up the corresponding powers of 2.
PHP
<?php function binToDec( $binNum ) { $decNum = 0; $len = strlen ( $binNum ); for ( $i = 0; $i < $len ; $i ++) { $decNum += (int) $binNum [ $i ] * pow(2, $len - $i - 1); } return $decNum ; } $binNum = '110010' ; $decNum = binToDec( $binNum ); echo "Decimal Number: " . $decNum ; ?> |
Output
Decimal Number: 50
Binary to Decimal Conversion in PHP
Given a binary number, the task is to convert Binary to Decimal Number in PHP.
Examples:
Input: 1010
Output: 10
Input: 11001
Output: 25
There are different methods to convert Binary to Decimal Numbers, These are:
Table of Content
- Using bindec() Function
- Using base_convert() Function
- Using Conversion Algorithm
Contact Us