Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Mathematical Functions : bindec

bindec

Binary to decimal (PHP 4, PHP 5)
number bindec ( string binary_string )

Returns the decimal equivalent of the binary number represented by the binary_string argument.

bindec() converts a binary number to an integer or, if needed for size reasons, float.

Parameters

binary_string

The binary string to convert

Return Values

The decimal value of binary_string

ChangeLog

Version Description
Since 4.1.0 The function can now convert numbers that are too large to fit into the platforms integer type, larger values are returned as float in that case.

Examples

Example 1139. bindec() example

<?php
echo bindec('110011') . "\n";
echo
bindec('000110011') . "\n";

echo
bindec('111');
?>

The above example will output:

51
51
7


Related Examples ( Source code ) » bindec



Code Examples / Notes » bindec

da_he4datthe-gurusdotde

[Editor's Note:
Numeric values starting with 0 are interpreted as octal (base eight) values.  For example: 0101 is converts to 65
--zak@php.net]
i was quite confused while werking with those bindec()/decbin() functions..
while
echo decbin(bindec(1));
echo decbin(bindec(10));
echo decbin(bindec(101));
just worked like expected,
echo decbin(bindec(010));
returns just 0
and for any reason
echo decbin(bindec(010010010));
returns 101000
i dont think this is a bug (maybe theres some special reason 4 this?), but it took  a certain time till i noticed that
echo decbin(bindec("010010010"));
works fine ...
hope this helps ne1.


gwbdome

i think a better method than the "shift-method" is my method ^^...
here it comes:
function convert2bin($string) {
    $finished=0;
    $base=1;
    if(preg_match("/[^0-9]/", $string)) {
        for($i=0; $string!=chr($i); $i++);
        $dec_nr=$i;
    }
    else $dec_nr=$string;
    while($dec_nr>$base) {
        $base=$base*2;
        if($base>$dec_nr) {
            $base=$base/2;
            break;
        }
    }
    while(!$finished) {
        if(($dec_nr-$base)>0) {
            $dec_nr=$dec_nr-$base;
            $bin_nr.=1;
            $base=$base/2;
        }
        elseif(($dec_nr-$base)<0) {
            $bin_nr.=0;
            $base=$base/2;
        }
        elseif(($dec_nr-$base)==0) {
            $bin_nr.=1;
            $finished=1;
            while($base>1) {    
                $bin_nr.=0;
                $base=$base/2;
            }
        }
    }
    return $bin_nr;
}
=====================================================
if you want to reconvert it (from binary to string or integer) you can use this function:
function reconvert($bin_nr) {
    $base=1;
    $dec_nr=0;
    $bin_nr=explode(",", preg_replace("/(.*),/", "$1", str_replace("1", "1,", str_replace("0", "0,", $bin_nr))));
    for($i=1; $i<count($bin_nr); $i++) $base=$base*2;
    foreach($bin_nr as $key=>$bin_nr_bit) {
        if($bin_nr_bit==1) {
            $dec_nr+=$base;
            $base=$base/2;
        }
        if($bin_nr_bit==0) $base=$base/2;
    }
    return(array("string"=>chr($dec_nr), "int"=>$dec_nr));
}


php

For converting larger-than-31-bit numbers:
<?php
function Bin2Dec($binstring) {
for ($i=0;$i<strlen($binstring);$i++) {
$decvalue += ((int) substr($binstring, strlen($binstring) - $i - 1, 1)) * pow(2, $i);
}
return $decvalue;
}
?>


26-aug-2004 10:42

decbin('1001') is prefered as decbin(1001).
Because on larger bit string, it may cause problem :
decbin(100000000000000) return 3
else
decbin('100000000000000') return 16384


juancri

> The special reason 4 this is:
> (010) is an integer, left 0 is null, eg 010 = 10
> ('010') is a string, eg '010' = '010'
In binary, just like with decimals, the left 0's are nulls.
in Decimal:  010 = 10
in Binary:    010 = 10 too :o)
> echo decbin(bindec(010010010));
> returns 101000
010010010 is a octal number. It's eq. to 2101256 (decimal). decbin(bindec(010010010)) is 101000 because 010010010 = 2101256.
That's all =)


martin

## calculate binary with "shift-method" ##
<?php
function dec2bin($decimal_code){
for($half=($decimal_code);$half>=1;$half=(floor($half))/2){
  if(($half%2)!=0){
   $y.=1;
  }
  else{
   $y.=0;
  }
 }
$calculated_bin=strrev($y);
return $calculated_bin;
}
?>
## example ##
[bin] 123 = [dec] 1111011
e.g.
123/2 = 61,5 => 1
61/2  = 30,5 => 1
30/2  = 15   => 0
15/2  = 7,5  => 1
7/2   = 3,5  => 1
3/2   = 1,5  => 1
1/2   = 0,5  => 1
(0/2   = 0    finish)


Change Language


Follow Navioo On Twitter
abs
acos
acosh
asin
asinh
atan2
atan
atanh
base_convert
bindec
ceil
cos
cosh
decbin
dechex
decoct
deg2rad
exp
expm1
floor
fmod
getrandmax
hexdec
hypot
is_finite
is_infinite
is_nan
lcg_value
log10
log1p
log
max
min
mt_getrandmax
mt_rand
mt_srand
octdec
pi
pow
rad2deg
rand
round
sin
sinh
sqrt
srand
tan
tanh
eXTReMe Tracker