php decbin函數(shù)的進(jìn)制轉(zhuǎn)換技巧有哪些

PHP
小樊
82
2024-08-10 06:16:34

  1. 使用decbin函數(shù)將十進(jìn)制數(shù)字轉(zhuǎn)換為二進(jìn)制數(shù)字
$decimal_number = 10;
$binary_number = decbin($decimal_number);
echo $binary_number; // 輸出: 1010
  1. 使用sprintf函數(shù)將十進(jìn)制數(shù)字轉(zhuǎn)換為指定位數(shù)的二進(jìn)制數(shù)字
$decimal_number = 10;
$binary_number = sprintf("%08b", $decimal_number);
echo $binary_number; // 輸出: 00001010
  1. 使用str_pad函數(shù)將十進(jìn)制數(shù)字轉(zhuǎn)換為指定位數(shù)的二進(jìn)制數(shù)字
$decimal_number = 10;
$binary_number = base_convert($decimal_number, 10, 2);
$binary_number = str_pad($binary_number, 8, "0", STR_PAD_LEFT);
echo $binary_number; // 輸出: 00001010

0