Math bindec
(PHP 4, PHP 5, PHP 7, PHP 8)
bindec — Binary to decimal
설명
bindec(string $binary_string
): int|float
binary_string
인수가 나타내는 이진수에 해당하는 10진수를 반환합니다.
bindec()은 이진수를 int로 변환하거나 크기상의 이유로 필요한 경우 float로 변환합니다.
bindec()은 모든 binary_string
값을 부호 없는 정수로 해석합니다. 이는 bindec()이 최상위 비트를 부호 비트가 아닌 다른 차원으로 보기 때문입니다.
매개변수
binary_string
- 변환할 이진 문자열입니다.
binary_string
의 유효하지 않은 문자는 자동으로 무시됩니다. PHP 7.4.0부터 유효하지 않은 문자를 제공하는 것은 더 이상 사용되지 않습니다.
경고 매개변수는 문자열이어야 합니다. 다른 데이터 유형을 사용하면 예기치 않은 결과가 발생합니다.
반환 값
binary_string
의 10진수 값
변경 로그
버전 | 설명 |
---|---|
7.4.0 | 유효하지 않은 문자를 전달하면 이제 사용 중단 알림이 생성됩니다. 결과는 유효하지 않은 문자가 존재하지 않는 것처럼 계속 계산됩니다. |
Examples
예제 #1 bindec() 예제
<?php
echo bindec('110011') . "\n";
echo bindec('000110011') . "\n";
echo bindec('111');
?>
위의 예는 다음을 출력합니다.
51 51 7
예제 #2 bindec()은 입력을 부호 없는 정수로 해석합니다.
<?php
/*
* The lesson from this example is in the output
* rather than the PHP code itself.
*/
$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() - Decimal to binary
- octdec() - Octal to decimal
- hexdec() - Hexadecimal to decimal
- base_convert() - Convert a number between arbitrary bases