(PHP 4, PHP 5, PHP 7)
bindec — 锟斤拷锟斤拷锟斤拷转锟斤拷为十锟斤拷锟斤拷
锟斤拷锟斤拷
binary_string
锟斤拷锟斤拷锟斤拷锟斤拷示锟侥讹拷锟斤拷锟斤拷锟斤拷锟斤拷十锟斤拷锟狡等硷拷值锟斤拷
bindec() 锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷 integer锟斤拷锟斤拷锟竭筹拷锟节达拷小锟斤拷锟斤拷要锟斤拷转锟斤拷为 float 锟斤拷锟酵★拷
bindec() 锟斤拷锟斤拷锟叫碉拷
binary_string
值锟斤拷锟斤拷为锟睫凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
bindec() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟叫伙拷锟轿拷锟斤拷锟斤拷锟斤拷锟斤拷欠锟斤拷锟轿伙拷锟�
binary_string
要转锟斤拷锟侥讹拷锟斤拷锟斤拷锟街凤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷为锟街凤拷锟斤拷锟斤拷使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟酵会导锟铰诧拷锟斤拷预知锟侥斤拷锟斤拷锟�
binary_string
锟斤拷十锟斤拷锟斤拷锟斤拷值
锟芥本 | 说锟斤拷 |
---|---|
4.1.0 | 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷平台锟斤拷 integer 锟斤拷锟斤拷锟斤拷锟街碉拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟街碉拷岱碉拷锟轿� float 锟斤拷锟酵★拷 |
Example #1 bindec() 锟斤拷锟斤拷
<?php
echo bindec('110011') . "\n";
echo bindec('000110011') . "\n";
echo bindec('111');
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
51 51 7
Example #2 bindec() 锟斤拷锟斤拷锟斤拷锟斤拷为锟睫凤拷锟斤拷锟斤拷锟斤拷
<?php
/*
* The lesson from this example is in the output
* rather than the PHP code itself.
*/
$magnitude_lower = pow(2, (PHP_INT_SIZE * 8) - 2);
p($magnitude_lower - 1);
p($magnitude_lower, 'See the rollover? Watch it next time around...');
p(PHP_INT_MAX, 'PHP_INT_MAX');
p(~PHP_INT_MAX, 'interpreted to be one more than PHP_INT_MAX');
if (PHP_INT_SIZE == 4) {
$note = 'interpreted to be the largest unsigned integer';
} else {
$note = 'interpreted to be the largest unsigned integer
(18446744073709551615) but skewed by float precision';
}
p(-1, $note);
function p($input, $note = '') {
echo "input: $input\n";
$format = '%0' . (PHP_INT_SIZE * 8) . 'b';
$bin = sprintf($format, $input);
echo "binary: $bin\n";
ini_set('precision', 20); // For readability on 64 bit boxes.
$dec = bindec($bin);
echo 'bindec(): ' . $dec . "\n";
if ($note) {
echo "NOTE: $note\n";
}
echo "\n";
}
?>
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 32 位锟斤拷锟斤拷锟较碉拷锟斤拷锟�:
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
锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 64 位锟斤拷锟斤拷锟较碉拷锟斤拷锟�:
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