bindec():Decimal equivalent of Binary string

Convert binary to decimal

bindec() reads a binary string and returns its decimal value. Keep the input limited to binary digits when accepting user-supplied values.

$binary = '101101';
echo bindec($binary); // 45
echo bindec('1101'); // Output 13 
echo "<br>";
echo bindec('0101'); // Output 5 
echo "<br>";
echo bindec('10110'); // Output is 22
echo "<br>";
echo bindec('101101101'); // Output is 365
echo "<br>";
echo bindec('111011100'); // Output is 476
echo "<br>"; 
Syntax
int|float bindec(string $binary);
ParameterDESCRIPTION
$binaryRequired : Binary number as Input string representing number to convert to equivalent decimal number.
The output is number converted to decimal base system.
Warning Pass the binary value as a string and validate that it contains only 0 and 1 when it comes from external input.

Example: Binary to Decimal with Leading Zeros

$binary = "000101";
echo bindec($binary);  // Output: 5

Example: Large Binary Numbers

$binary = "110010101011011010";
echo bindec($binary);  // Output: 207610

Validate a binary string before conversion

$binary = "1011";
if (preg_match('/^[01]+$/', $binary)) {
    echo bindec($binary);  // Output: 11
}

Example: Binary Conversion with Spaces

$binary = "1010 0011";  // Spaces between bits
$binary_clean = str_replace(' ', '', $binary);  // Remove spaces
echo bindec($binary_clean);  // Output: 163

Example: Using bindec() with Binary String in Variable

$binary = "1001011";
$decimal = bindec($binary);
echo "Binary: $binary to Decimal: $decimal";  // Output: Binary: 1001011 to Decimal: 75

Example: Converting Binary Fractions

$binary = "101.101";
list($intPart, $fracPart) = explode('.', $binary);
$decimal = bindec($intPart) + bindec($fracPart) / pow(2, strlen($fracPart));
echo $decimal;  // Output: 5.625

Math Functions base_convert(): Convert number From any base to other
decbin() Binary equivalent of decimal number


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer