<?php
int|float bindec ( string $binary_string )
where,
$binary_string = The binary value as a STRING
to be converted to decimal
?>
<?php
$binstr_01 =
'11111111111111111111111111111111';
$binstr_02 =
'1111111111111111111111111111111111111111111111';
$binstr_03 = decbin(PHP_INT_MIN);
$binstr_04 = decbin(PHP_INT_MAX);
$arr_01_04 = [ $binstr_01, $binstr_02,
$binstr_03, $binstr_04 ];
foreach ($arr_01_04 as $nbr)
{
echo 'bindec( ' . $nbr . ' ) =<br>= ' .
bindec($nbr) . '<br><br>';
}
?>
<?php
/*
if (PHP_INT_SIZE != 4)
die("skip this test is for 32bit platform only");
*/
$values = array(111000111,
011100000,
1111111111111111111111111111111,
10000000000000000000000000000000,
100002001,
'111000111',
'011100000',
'1111111111111111111111111111111',
'10000000000000000000000000000000',
'100002001',
'abcdefg',
311015,
31101.3,
31.1013e5,
0x111ABC,
011237,
true,
false,
null);
// Deprecated:
// Invalid characters passed
// for attempted conversion,
// these have been ignored
for ($i = 0; $i < count($values); $i++) {
$res = bindec($values[$i]);
var_dump($res);
echo '<br><br>';
}
?>
<?php
if (PHP_INT_SIZE != 8)
die("skip this test is for 64bit platform only");
define("MAX_64Bit", 9223372036854775807);
define("MAX_32Bit", 2147483647);
define("MIN_64Bit", -9223372036854775807 - 1);
define("MIN_32Bit", -2147483647 - 1);
$binLongStrs = array(
'0'.str_repeat('1',63),
str_repeat('1',64),
'0'.str_repeat('1',31),
str_repeat('1',32),
'0'.str_repeat('1',64),
str_repeat('1',65),
'0'.str_repeat('1',32),
str_repeat('1',33)
);
foreach ($binLongStrs as $strVal) {
echo "Testing: $strVal<br>";
var_dump(bindec($strVal));
echo '<br><br>';
}
?>
<?php
if (PHP_INT_SIZE != 8)
die("skip this test is for 64bit platform only");
$values = array(111000111,
011100000,
1111111111111111111111111111111,
10000000000000000000000000000000,
100002001,
'111000111',
'011100000',
'1111111111111111111111111111111',
'10000000000000000000000000000000',
'100002001',
'abcdefg',
311015,
31101.3,
31.1013e5,
0x111ABC,
011237,
true,
false,
null);
// Deprecated:
// Invalid characters passed
// for attempted conversion,
// these have been ignored
for ($i = 0; $i < count($values); $i++) {
$res = bindec($values[$i]);
var_dump($res);
echo '<br><br>';
}
?>
<?php
/*
if (PHP_INT_SIZE != 4)
die("skip this test is for 32bit platform only");
*/
echo "Testing bindec() : usage variations<br>";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
abc
xyz
EOT;
// get a resource variable
$fp = fopen(__FILE__, "r");
$inputs = array(
// int data
/*1*/ 0,
1,
12345,
-2345,
// float data
/*5*/ 10.5,
-10.5,
12.3456789000e10,
12.3456789000E-10,
.5,
// null data
/*10*/ NULL,
null,
// boolean data
/*12*/ true,
false,
TRUE,
FALSE,
// empty data
/*16*/ "",
'',
array(),
// string data
/*19*/ "abcxyz",
'abcxyz',
$heredoc,
// undefined data
/*22*/ @$undefined_var,
// unset data
/*23*/ @$unset_var,
// resource variable
/*24*/ $fp
);
// loop through each element of
// $inputs to check the behaviour
// of bindec()
// Deprecated:
// Invalid characters passed
// for attempted conversion,
// these have been ignored
$iterator = 1;
foreach($inputs as $input) {
echo "<br>Iteration: $iterator<br>";
try {
var_dump(bindec($input));
echo '<br><br>';
} catch (TypeError $e) {
echo $e->getMessage(), "<br><br>";
}
$iterator++;
};
fclose($fp);
?>
<?php
if (PHP_INT_SIZE != 8)
die("skip this test is for 64bit platform only");
echo "Testing bindec() : usage variations<br>";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
abc
xyz
EOT;
// get a resource variable
$fp = fopen(__FILE__, "r");
$inputs = array(
// int data
/*1*/ 0,
1,
12345,
-2345,
// float data
/*5*/ 10.5,
-10.5,
12.3456789000e10,
12.3456789000E-10,
.5,
// null data
/*10*/ NULL,
null,
// boolean data
/*12*/ true,
false,
TRUE,
FALSE,
// empty data
/*16*/ "",
'',
array(),
// string data
/*19*/ "abcxyz",
'abcxyz',
$heredoc,
// undefined data
/*22*/ @$undefined_var,
// unset data
/*23*/ @$unset_var,
// resource variable
/*24*/ $fp
);
// loop through each element of
// $inputs to check the behaviour of
// bindec()
// Deprecated:
// Invalid characters passed
// for attempted conversion,
// these have been ignored
$iterator = 1;
foreach($inputs as $input) {
echo "<br>Iteration $iterator<br><br>";
try {
var_dump(bindec($input));
} catch (TypeError $e) {
echo $e->getMessage(), "<br><br>";
}
$iterator++;
};
fclose($fp);
?>
<?php
$binstr_e = '10111011010';
echo 'bindec( ' . $binstr_e . ' ) = ' .
bindec($binstr_e) . '<br>';
?>