The bindec()
function in PHP converts a binary string (base-2) into a decimal number (base-10).
It accepts a string of 0s and 1s and returns the equivalent integer or float.
This is useful for working with binary data, flags, permissions, or bitwise operations.
It’s the opposite of decbin()
, which converts decimal to binary.
Example: bindec("1101")
returns 13
.
Syntax
bindec(string $binary_string): int|float
- $binary_string: A string containing a valid binary number (only
0
s and1
s) - Returns: The decimal (base-10) value as an
int
orfloat
File Name: bindec-example.php
<?php
echo bindec("1101"); // ➤ 13
echo "\n";
echo bindec("101010"); // ➤ 42
echo "\n";
echo bindec("11111111"); // ➤ 255
?>
Use Cases
Use Case | Description |
---|---|
Binary to decimal | For decoding binary input or file headers |
Low-level bit operations | Handling flags, bitmasks, permissions |
Networking | IP subnet masks, binary headers |
Summary
Function | Description |
---|---|
bindec() |
Converts binary to decimal |
Input | Binary string (e.g. "1010" ) |
Output | Decimal integer or float |