note 32519 deleted from function.bindec by irchtml
| From: | irchtml@php.net | Date: | Sat, 16 Oct 2004 09:17:09 +0000 |
| Subject: | note 32519 deleted from function.bindec by irchtml | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-78654@lists.php.net to get a copy of this message | ||
Note Submitter: martin at punix dot de
----
## calculate the dec with the "shift-method" ##
<?php
function bin2dec($binary_code){
for($i=1;$i<=strlen($binary_code);$i++){
if ($binary_code{$i-1} == "1"){
$x=(2*$x)+1;
}
else{
$x=2*$x;
}
}
$calculated_dec=$x;
return $calculated_dec;
}
?>
## Example ##
1011 -> 11 | End: x = (x*2)+1
101 -> 5 | .. x = (x*2)+1
10 -> 2 | .. x = x*2
1 -> 1 | Start: x = 1
(0 -> 0) |