2024 年 PHP Conference Japan

decbin

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

decbin十進位轉二進位

說明

decbin(int $num): string

傳回一個字串,包含給定 num 參數的二進位表示法。

參數

num

要轉換的十進位值

32 位元機器上的輸入範圍
num num 返回值
0   0
1   1
2   10
... 正常遞增 ...
2147483646   1111111111111111111111111111110
2147483647 (最大的帶符號整數)   1111111111111111111111111111111 (31 個 1)
2147483648 -2147483648 10000000000000000000000000000000
... 正常遞增 ...
4294967294 -2 11111111111111111111111111111110
4294967295 (最大的無號整數) -1 11111111111111111111111111111111 (32 個 1)
64 位元機器上的輸入範圍
num num 返回值
0   0
1   1
2   10
... 正常遞增 ...
9223372036854775806   111111111111111111111111111111111111111111111111111111111111110
9223372036854775807 (最大的有號整數)   111111111111111111111111111111111111111111111111111111111111111 (63 個 1)
  -9223372036854775808 1000000000000000000000000000000000000000000000000000000000000000
... 正常遞增 ...
  -2 1111111111111111111111111111111111111111111111111111111111111110
  -1 1111111111111111111111111111111111111111111111111111111111111111 (64 個 1)

返回值

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 作為格式

新增筆記

使用者貢獻的筆記 14 則筆記

MarcelG
10 年前
要新增前導零,我偏好以下方式
<?php
// 新增前導零
$bin = sprintf( "%08d", decbin( 26 )); // "00011010"
?>
rambabusaravanan at gmail dot com
7 年前
用一個簡單的語句將帶有前導零的二進位格式列印到變數中。

<?php
$binary
= sprintf('%08b', $decimal); // $decimal = 5;
echo $binary; // $binary = "00000101";
?>
coverup
7 年前
關於尾隨零,在測試了其他人在此處提到的所有選項後,我自行進行了效率測試,結果如下

<?php

$decimal
= 9;

$time_start = microtime(true);
for (
$i=0;$i<1000;$i++){
$bin = printf('%08b', $decimal);
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo
"<hr>耗時 $time 秒<br>\n";
echo
$bin . '<br>';

$time_start = microtime(true);
for (
$i=0;$i<1000;$i++){
$bin = sprintf( "%08d", decbin( $decimal ));
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo
"<hr>耗時 $time 秒<br>\n";
echo
$bin . '<br>';

$time_start = microtime(true);
for (
$i=0;$i<1000;$i++){
$bin = decbin($decimal);
$bin = substr("00000000",0,8 - strlen($bin)) . $bin;
}
$time_end = microtime(true);
$time = $time_end - $time_start;
echo
"<hr>耗時 $time 秒<br>\n";
echo
$bin . '<br>';

?>

結果

0000100100001001000010010000100.... (輸出重複 1000 次)
耗時 0.0134768486023 秒
8
耗時 0.00054407119751 秒
00001001
耗時 0.000833988189697 秒
00001001

因此,最佳的方案是
<?php
$bin
= sprintf( "%08d", decbin( $decimal ));
?>
Xavier Daull
18 年前
一個快速將二進制字串轉換為位元序列的函式

<?php

function BinString2BitSequence($mystring) {
$mybitseq = "";
$end = strlen($mystring);
for(
$i = 0 ; $i < $end; $i++){
$mybyte = decbin(ord($mystring[$i])); // 將字元轉換為位元字串
$mybitseq .= substr("00000000",0,8 - strlen($mybyte)) . $mybyte; // 8 位元打包
}
return
$mybitseq;
}

echo
BinString2BitSequence("ABCDEF"); // 輸出=010000010100001001000011010001000100010101000110

?>
匿名
18 年前
只是一個例子
如果您將 26 轉換為二進制,您將得到 11010,它是 5 個字元長。如果您需要完整的 8 位元值,請使用以下程式碼

$bin = decbin(26);
$bin = substr("00000000",0,8 - strlen($bin)) . $bin;

這會將 11010 轉換為 00011010。
noname at example dot com
9 年前
如果您想要前導零,請使用 PHP 內建功能,而不是自訂函式

<?php
printf
('%08b', $decimal);
?>

>> printf('%08b', E_NOTICE)
>> 00001000
dj.luiting(.a.t.)gmail.com
10 年前
大家好,我在 Windows 平台上花了一天時間才將一個很大的十進制數轉換為二進制。
最後,使用 bcmath 函式,這就是對我有用的方法。

function bc_convert2bin($string) {

//使用 bcmath 函式使其工作,適用於 32 位元 Windows 機器上的 64 位元
$finished=0;
$base=2;
$bin_nr='';

if(preg_match("/[^0-9]/", $string)) {
for($i=0; $string!=chr($i); $i++) {
$dec_nr=$i;
}
} else {
$dec_nr=$string;
}
//while( $dec_nr>$base ) {
while( bccomp($dec_nr,$base) == 1 ) {

//$base=$base*2;
$base=bcmul($base,'2');
//if($base>$dec_nr) {
if( bccomp($base,$dec_nr) == 1 ) {
//$base=$base/2;
$base=bcdiv($base,'2');
break;
}
}

while(!$finished) {

//if(($dec_nr-$base)>0) {
if( bccomp( bcsub($dec_nr,$base) , 0) == 1 ) {

//$dec_nr=$dec_nr-$base;
$dec_nr=bcsub($dec_nr,$base);
$bin_nr.=1;
//$base=$base/2;
$base=bcdiv($base,'2');

//} elseif(($dec_nr-$base)<0) {
} elseif( bccomp( bcsub($dec_nr,$base) , 0) == -1 ) {

$bin_nr.=0;
//$base=$base/2;
$base=bcdiv($base,'2');

//} elseif(($dec_nr-$base)==0) {
} elseif( bccomp( bcsub($dec_nr,$base) , 0) == 0 ) {

$bin_nr.=1;
$finished=1;
//while($base>1) {
while( bccomp($base,1) == 1 ) {

$bin_nr.=0;
//$base=$base/2;
$base=bcdiv($base,'2');

}
}
}
return $bin_nr;
}
malcolm.murphy
7 年前
GNU MP 函式庫 (https://php.dev.org.tw/manual/en/book.gmp.php) 提供了將任意長度的二進位字串有效地轉換為其二進位表示形式的方法(即相當於字串的 `decbin` 函式)。

<?php

$str
= random_bytes(1024); // 二進位字串範例

$result = gmp_strval(gmp_import($str), 2); // 參考手冊了解 endianness 等選項

$zeroPadded = sprintf('%0' . (strlen($str) * 8) . 's', $result); // 必要時進行零填充,例如使用 str_pad 或如所示的 sprintf

$strAgain = gmp_export(gmp_init($result, 2)); // 類似 bindec 的反向操作

?>
php at silisoftware dot com
22 年前
另一個大於 31 位元的函式。
適用於非常大的數字,但隨著大小增加會犧牲完美的位元精度(我注意到超過 16 位小數位左右就會出現捨入誤差),因此請謹慎使用,並且僅在 decbin() 無法處理時使用。

function Dec2Bin($number) {
while ($number >= 256) {
$bytes[] = (($number / 256) - (floor($number / 256))) * 256;
$number = floor($number / 256);
}
$bytes[] = $number;
for ($i=0;$i<count($bytes);$i++) {
$binstring = (($i == count($bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, "0", STR_PAD_LEFT)).$binstring;
}
return $binstring;
}
Nitrogen
15 年前
使用 BCMath 擴充套件進行十進位到二進位的轉換。

<?php

function BCDec2Bin($Input='') {
$Output='';
if(
preg_match("/^\d+$/",$Input)) {
while(
$Input!='0') {
$Output.=chr(48+($Input{strlen($Input)-1}%2));
$Input=BCDiv($Input,'2');
}
$Output=strrev($Output);
}
return((
$Output!='')?$Output:'0');
}

?>

這將使用 BCMath(任意精度計算)簡單地從十進位轉換為二進位。

另請參閱:我在「bindec」文件中撰寫的「BCBin2Dec」函式。
敬請享用,
Nitrogen.
mcampa at gmail dot com
11 年前
一個好用的小函式,可以回傳帶有前導零的二進位字串

function d2b($n) {
return str_pad(decbin($n), 16, "0", STR_PAD_LEFT);
}

// 範例
echo d2b(E_ALL);
echo d2b(E_ALL | E_STRICT);
echo d2b(0xAA55);
echo d2b(5);

輸出
0111011111111111
0111111111111111
1010101001010101
0000000000000101
ajl at gmx dot de
23 年前
這裡提供一個可以轉換 64 位元而非 32 位元的版本,使用標準的 decbin 函式
<?
function bigdecbin($dec,$doublewords=1) {
$erg = "";
do {
$rest = $dec%2147483648;
if ($rest<0) $rest+=2147483648;
$erg = str_pad(decbin($rest),31,"0",STR_PAD_LEFT).$erg;
$dec = ($dec-$rest)/2147483648;
} while (($dec>0)&&(!($dec<1)));

return str_pad($erg,$doublewords*31,"0",STR_PAD_LEFT);
}

echo "<pre>";
for ($i=1.5*2147483647.0-10;$i<1.5*2147483647.0+10;$i++) {
echo "十進位:".$i." 二進位:".bigdecbin($i,2)."<br>";
}
echo "</pre>";
?>
匿名
19 年前
base_convert( base_convert('100001000100000000010001001000
0100100000001111111111111111111',2,10),10,2);

回傳
'1000010001000000000100010010000
100100000010000000000000000000'

這個函式無效
darkshad3 at yahoo dot com
17 年前
<?php
Print bindecValues("1023");

function
bindecValues($decimal, $reverse=false, $inverse=false) {
/*
1. 這個函式接受一個十進位數,將其轉換為二進位,並返回二進位字串中每個個別二進位值(1)的十進位值。
您可以使用更大的十進位值,如果您將它們作為字串傳遞給函式!
2. 第二個可選參數反轉輸出。
3. 第三個可選參數反轉二進位字串,例如 101 變為 010。
-- darkshad3 at yahoo dot com
*/

$bin = decbin($decimal);
if (
$inverse) {
$bin = str_replace("0", "x", $bin);
$bin = str_replace("1", "0", $bin);
$bin = str_replace("x", "1", $bin);
}
$total = strlen($bin);

$stock = array();

for (
$i = 0; $i < $total; $i++) {
if (
$bin{$i} != 0) {
$bin_2 = str_pad($bin{$i}, $total - $i, 0);
array_push($stock, bindec($bin_2));
}
}

$reverse ? rsort($stock):sort($stock);
return
implode(", ", $stock);
}
?>

輸出的結果是:1, 2, 4, 8, 16, 32, 64, 128, 256, 512
To Top