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.(PHP 4, PHP 5, PHP 7, PHP 8)
decbin — Da decimale a binario
Restituisce una stringa contenente una rappresentazione binaria di un dato
argomento numero. Il più grande numero che può essere convertito è
4294967295 in decimale, risultante in una stringa composta da 32 volte la cifra 1.
Example #1 Esempio per decbin()
<?php
echo decbin(12) . "\n";
echo decbin(26);
?>L'esempio precedente visualizzerà:
1100 11010
Vedere anche bindec(), decoct(), dechex() e base_convert().
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.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";
?>