decbin

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

decbin10 進数を 2 進数に変換する

説明

decbin(int $num): string

引数 num を 2 進数表現した文字列を返します。

パラメータ

num

変換したい 10 進数値。

32 ビットマシンでの入力の範囲
正の num 負の num 戻り値
0   0
1   1
2   10
... normal progression ...
2147483646   1111111111111111111111111111110
2147483647 (符号付き integer の最大値)   1111111111111111111111111111111 (1 が 31 個)
2147483648 -2147483648 10000000000000000000000000000000
... normal progression ...
4294967294 -2 11111111111111111111111111111110
4294967295 (符号なし integer の最大値) -1 11111111111111111111111111111111 (1 が 32 個)
64 ビットマシンでの入力の範囲
正の num 負の num 戻り値
0   0
1   1
2   10
... normal progression ...
9223372036854775806   111111111111111111111111111111111111111111111111111111111111110
9223372036854775807 (符号付き integer の最大値)   111111111111111111111111111111111111111111111111111111111111111 (1 が 63 個)
  -9223372036854775808 1000000000000000000000000000000000000000000000000000000000000000
... normal progression ...
  -2 1111111111111111111111111111111111111111111111111111111111111110
  -1 1111111111111111111111111111111111111111111111111111111111111111 (1 が 64 個)

戻り値

num を 2 進文字列で表した値を返します。

例1 decbin() の例

<?php
echo decbin(12) . "\n";
echo
decbin(26);
?>

上の例の出力は以下となります。

1100
11010

参考

  • bindec() - 2 進数 を 10 進数に変換する
  • decoct() - 10 進数を 8 進数に変換する
  • dechex() - 10 進数を 16 進数に変換する
  • base_convert() - 数値の基数を任意に変換する
  • printf() - フォーマット済みの文字列を出力する でのフォーマット %b%032b あるいは %064b
  • sprintf() - フォーマットされた文字列を返す でのフォーマット %b%032b あるいは %064b

add a note

User Contributed Notes 2 notes

up
8
rambabusaravanan at gmail dot com
8 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
19 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.
To Top