note 81793 added to function.bindec
| From: | mashematicianatgmaildotcom at osu1 dot php dot net | Date: | Thu, 13 Mar 2008 16:57:42 +0000 |
| Subject: | note 81793 added to function.bindec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-137028@lists.php.net to get a copy of this message | ||
A binary to decimal conversion function that takes advantage of the BC library functions to return
decimal values of arbitrary length.
Input type must be a string in order to work properly.
<?php
function binary_to_decimal($a) {
$bin_array = str_split($a);
$y=sizeof($bin_array)-1;
for ($x=0; $x<sizeof($bin_array)-1; $x++) {
if ($bin_array[$x] == 1) {
$bin_array[$x] = bcpow(2, $y);
}
$y--;
}
for ($z=0; $z<sizeof($bin_array); $z++) {
$result = bcadd($result, $bin_array[$z]);
}
echo $result;
}
binary_to_decimal('11111');
?>
----
Server IP: 64.71.164.2
Probable Submitter: 63.125.147.253
----
Manual Page -- http://www.php.net/manual/en/function.bindec.php
Edit -- https://master.php.net/note/edit/81793
Del: integrated -- https://master.php.net/note/delete/81793/integrated
Del: useless -- https://master.php.net/note/delete/81793/useless
Del: bad code -- https://master.php.net/note/delete/81793/bad+code
Del: spam -- https://master.php.net/note/delete/81793/spam
Del: non-english -- https://master.php.net/note/delete/81793/non-english
Del: in docs -- https://master.php.net/note/delete/81793/in+docs
Del: other reasons-- https://master.php.net/note/delete/81793
Reject -- https://master.php.net/note/reject/81793
Search -- https://master.php.net/manage/user-notes.php