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 — Decimal to binary
Returns a string containing a binary representation of the given
num
argument.
num
Decimal value to convert
positive num |
negative num |
return value |
---|---|---|
0 | 0 | |
1 | 1 | |
2 | 10 | |
... normal progression ... | ||
2147483646 | 1111111111111111111111111111110 | |
2147483647 (largest signed integer) | 1111111111111111111111111111111 (31 1's) | |
2147483648 | -2147483648 | 10000000000000000000000000000000 |
... normal progression ... | ||
4294967294 | -2 | 11111111111111111111111111111110 |
4294967295 (largest unsigned integer) | -1 | 11111111111111111111111111111111 (32 1's) |
positive num |
negative num |
return value |
---|---|---|
0 | 0 | |
1 | 1 | |
2 | 10 | |
... normal progression ... | ||
9223372036854775806 | 111111111111111111111111111111111111111111111111111111111111110 | |
9223372036854775807 (largest signed integer) | 111111111111111111111111111111111111111111111111111111111111111 (63 1's) | |
-9223372036854775808 | 1000000000000000000000000000000000000000000000000000000000000000 | |
... normal progression ... | ||
-2 | 1111111111111111111111111111111111111111111111111111111111111110 | |
-1 | 1111111111111111111111111111111111111111111111111111111111111111 (64 1's) |
Binary string representation of num
Example #1 decbin() example
<?php
echo decbin(12) . "\n";
echo decbin(26);
?>
The above example will output:
1100 11010
%b
,
%032b
or %064b
as the format
%b
,
%032b
or %064b
as the format
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";
?>
Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!
Check out: eBank.nz (Art Generator) |
Netwrck.com (AI Tools) |
Text-Generator.io (AI API) |
BitBank.nz (Crypto AI) |
ReadingTime (Kids Reading) |
RewordGame |
BigMultiplayerChess |
WebFiddle |
How.nz |
Helix AI Assistant