Print as binary format with leading zeros into a variable in one simple statement.<?php $binary = sprintf('%08b', $decimal); // $decimal = 5; echo $binary; // $binary = "00000101";?>(PHP 4, PHP 5, PHP 7, PHP 8)
decbin — 十进制转换为二进制
num要转换的十进制值
正 num |
负 num |
返回值 |
|---|---|---|
| 0 | 0 | |
| 1 | 1 | |
| 2 | 10 | |
| ... normal progression ... | ||
| 2147483646 | 1111111111111111111111111111110 | |
| 2147483647(最大有符号整数) | 1111111111111111111111111111111 (31 1's) | |
| 2147483648 | -2147483648 | 10000000000000000000000000000000 |
| ... normal progression ... | ||
| 4294967294 | -2 | 11111111111111111111111111111110 |
| 4294967295(最大无符号整数) | -1 | 11111111111111111111111111111111 (32 1's) |
正 num |
负 num |
返回值 |
|---|---|---|
| 0 | 0 | |
| 1 | 1 | |
| 2 | 10 | |
| ... normal progression ... | ||
| 9223372036854775806 | 111111111111111111111111111111111111111111111111111111111111110 | |
| 9223372036854775807(最大有符号整数) | 111111111111111111111111111111111111111111111111111111111111111 (63 1's) | |
| -9223372036854775808 | 1000000000000000000000000000000000000000000000000000000000000000 | |
| ... normal progression ... | ||
| -2 | 1111111111111111111111111111111111111111111111111111111111111110 | |
| -1 | 1111111111111111111111111111111111111111111111111111111111111111 (64 1's) | |
num 的二进制字符串表示
示例 #1 decbin() 示例
<?php
echo decbin(12) . "\n";
echo decbin(26);
?>以上示例会输出:
1100 11010
Print as binary format with leading zeros into a variable in one simple statement.<?php $binary = sprintf('%08b', $decimal); // $decimal = 5; echo $binary; // $binary = "00000101";?>