PHP 8.2.32 Released!

decbin

(PHP 4, PHP 5, PHP 7, PHP 8)

decbin十进制转换为二进制

说明

function decbin(int $num): string

返回字符串,包含有给定 num 参数的二进制表示。

参数

num

要转换的十进制值

32 位机器上的输入范围
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)
64 位机器上的输入范围
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

参见

  • bindec() - 二进制转换为十进制
  • decoct() - 十进制转换为八进制
  • dechex() - 十进制转换为十六进制
  • base_convert() - 在任意进制之间转换数字
  • printf() - 输出格式化字符串,格式使用 %b%032b%064b
  • sprintf() - 返回格式化字符串,格式使用 %b%032b%064b

发现了问题?

了解如何改进此页面提交拉取请求报告一个错误
+添加备注

用户贡献的备注 2 notes

up
8
rambabusaravanan at gmail dot com
9 years ago
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";
?>
up
7
Anonymous
20 years ago
Just an example:
If you convert 26 to bin you'll get 11010, which is 5 chars long. If you need the full 8-bit value use this:
$bin = decbin(26);
$bin = substr("00000000",0,8 - strlen($bin)) . $bin;
This will convert 11010 to 00011010.
+添加备注

AltStyle によって変換されたページ (->オリジナル) /