bindec

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

bindec2 進数 を 10 進数に変換する

説明

bindec(string$binary_string): int|float

引数 binary_string により指定された 2 進数と等価な 10 進数を返します。

bindec() は、2 進数を int に変換します。 サイズの問題により、必要に応じて float となることもあります。

bindec() は、すべての binary_string 値を符号なし整数として扱います。 これは、bindec() が最上位ビットを 符号ビットではなく別の桁とみなすからです。

パラメータ

binary_string

変換したい 2 進数文字列。 binary_string に無効な文字を与えても、静かに無視されます。 PHP 7.4.0 以降では、無効な文字を与えることは推奨されません。

警告

このパラメータは文字列でなければなりません。 他のデータ型を使用すると、予期せぬ結果となります。

戻り値

binary_string を 10 進に変換した値を返します。

変更履歴

バージョン説明
7.4.0 無効な文字を与えると、非推奨の警告が出るようになりました。 結果は不正な文字がなかったかのように計算されます。

例1 bindec() の例

<?php
echo bindec('110011') . "\n";
echo
bindec('000110011') . "\n";

echo
bindec('111');
?>

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

51 51 7

例2 bindec() が入力を符号なし整数として処理する例

<?php


$magnitude_lower = pow(2, (PHP_INT_SIZE * 8) - 2);
p($magnitude_lower - 1);
p($magnitude_lower, 'See the rollover? Watch it next time around...');

p(PHP_INT_MAX, 'PHP_INT_MAX');
p(~PHP_INT_MAX, 'interpreted to be one more than PHP_INT_MAX');

if (
PHP_INT_SIZE == 4) {
$note = 'interpreted to be the largest unsigned integer';
} else {
$note = 'interpreted to be the largest unsigned integer
(18446744073709551615) but skewed by float precision'
;
}
p(-1, $note);


function
p($input, $note = '') {
echo
"input: $input\n";

$format = '%0' . (PHP_INT_SIZE * 8) . 'b';
$bin = sprintf($format, $input);
echo
"binary: $bin\n";

ini_set('precision', 20); // For readability on 64 bit boxes.
$dec = bindec($bin);
echo
'bindec(): ' . $dec . "\n";

if (
$note) {
echo
"NOTE: $note\n";
}

echo
"\n";
}
?>

上の例の 32 ビットマシンでの出力は、このようになります。

input: 1073741823 binary: 00111111111111111111111111111111 bindec(): 1073741823 input: 1073741824 binary: 01000000000000000000000000000000 bindec(): 1073741824 NOTE: See the rollover? Watch it next time around... input: 2147483647 binary: 01111111111111111111111111111111 bindec(): 2147483647 NOTE: PHP_INT_MAX input: -2147483648 binary: 10000000000000000000000000000000 bindec(): 2147483648 NOTE: interpreted to be one more than PHP_INT_MAX input: -1 binary: 11111111111111111111111111111111 bindec(): 4294967295 NOTE: interpreted to be the largest unsigned integer

上の例の 64 ビットマシンでの出力は、このようになります。

input: 4611686018427387903 binary: 0011111111111111111111111111111111111111111111111111111111111111 bindec(): 4611686018427387903 input: 4611686018427387904 binary: 0100000000000000000000000000000000000000000000000000000000000000 bindec(): 4611686018427387904 NOTE: See the rollover? Watch it next time around... input: 9223372036854775807 binary: 0111111111111111111111111111111111111111111111111111111111111111 bindec(): 9223372036854775807 NOTE: PHP_INT_MAX input: -9223372036854775808 binary: 1000000000000000000000000000000000000000000000000000000000000000 bindec(): 9223372036854775808 NOTE: interpreted to be one more than PHP_INT_MAX input: -1 binary: 1111111111111111111111111111111111111111111111111111111111111111 bindec(): 18446744073709551616 NOTE: interpreted to be the largest unsigned integer (18446744073709551615) but skewed by float precision

注意

注意:

この関数は、プラットフォームの int 型に収まらない大きな数も変換できます。 その場合、結果は float で返します。

参考

  • decbin() - 10 進数を 2 進数に変換する
  • octdec() - 8 進数を 10 進数に変換する
  • hexdec() - 16 進数を 10 進数に変換する
  • base_convert() - 数値の基数を任意に変換する
To Top