[PHP 4, PHP 5, PHP 7, PHP 8]
bindec — Binary to decimal
Description
bindec[string $binary_string
]: int|float
bindec[] converts a binary number to an int or, if needed for size reasons, float.
bindec[] interprets all binary_string
values as unsigned integers. This is because bindec[] sees the most significant bit as another order of magnitude rather than as the sign bit.
Parameters
binary_string
The binary string to convert. Any invalid characters in binary_string
are silently ignored. As of PHP 7.4.0 supplying any invalid characters is deprecated.
Warning
The parameter must be a string. Using other data types will produce unexpected results.
Return Values
The decimal value of binary_string
Changelog
7.4.0 | Passing invalid characters will now generate a deprecation notice. The result will still be computed as if the invalid characters did not exist. |
Examples
Example #1 bindec[] example
The above example will output:
Example #2 bindec[] interprets input as unsigned integers
Output of the above example on 32 bit machines:
input: 1073741823 binary: 00111111111111111111111111111111 bindec[]: 1073741823 input: 1073741824 binary: 01000000000000000000000000000000 bindec[]: 1073741824 NOTE: See the rollover? Watch it next time around... input: 2147483647 binary: 01111111111111111111111111111111 bindec[]: 2147483647 NOTE: PHP_INT_MAX input: -2147483648 binary: 10000000000000000000000000000000 bindec[]: 2147483648 NOTE: interpreted to be one more than PHP_INT_MAX input: -1 binary: 11111111111111111111111111111111 bindec[]: 4294967295 NOTE: interpreted to be the largest unsigned integer
Output of the above example on 64 bit machines:
input: 4611686018427387903 binary: 0011111111111111111111111111111111111111111111111111111111111111 bindec[]: 4611686018427387903 input: 4611686018427387904 binary: 0100000000000000000000000000000000000000000000000000000000000000 bindec[]: 4611686018427387904 NOTE: See the rollover? Watch it next time around... input: 9223372036854775807 binary: 0111111111111111111111111111111111111111111111111111111111111111 bindec[]: 9223372036854775807 NOTE: PHP_INT_MAX input: -9223372036854775808 binary: 1000000000000000000000000000000000000000000000000000000000000000 bindec[]: 9223372036854775808 NOTE: interpreted to be one more than PHP_INT_MAX input: -1 binary: 1111111111111111111111111111111111111111111111111111111111111111 bindec[]: 18446744073709551616 NOTE: interpreted to be the largest unsigned integer [18446744073709551615] but skewed by float precision
Notes
Note:
The function can convert numbers that are too large to fit into the platforms int type, larger values are returned as float in that case.
See Also
- decbin[] - Decimal to binary
- octdec[] - Octal to decimal
- hexdec[] - Hexadecimal to decimal
- base_convert[] - Convert a number between arbitrary bases
info at rickdg dot nl ¶
12 years ago
Two functions to convert 16bit or 8bit binary to integer using two's complement. If input exceeds maximum bits, false is returned. Function is easily scalable to x bits by changing the hexadecimals.
gwbdome at freenet dot de ¶
18 years ago
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]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$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]];
}
martin at punix dot de ¶
19 years ago
## calculate binary with "shift-method" ##
## 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]
Nitrogen ¶
13 years ago
Binary to Decimal conversion using the BCMath extension..
This will simply convert from Base-2 to Base-10 using BCMath [arbitrary precision calculation].
See also: my 'BCDec2Bin' function on the 'decbin' document.
Enjoy,
Nitrogen.
patrick dot boens at latosensu dot be ¶
10 years ago
Left-shift a string by a number of bytes
Right-shift a string by a number of bytes
Additional functions used by the two preceding:
mashematician at gmail dot com ¶
14 years ago
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.
alan hogan dot com slash contact ¶
14 years ago
The "smartbindec" function I wrote below will convert any binary string [of a reasonable size] to decimal. It will use two's complement if the leftmost bit is 1, regardless of bit length. If you are getting unexpected negative answers, try zero-padding your strings with sprintf["%032s", $yourBitString].
jpfstange at gmail dot com ¶
6 years ago