PHP Program to convert binary to decimal using bindec() function


March 11, 2022, Learn eTutorial
1294

How to convert the binary value into a decimal value?

To convert a binary number to decimal we have to find the power of each digit of the binary number according to their position which starts from 0 in reverse and add those together. For example if the entered number is 1011 then we calculate (1 × 2³) + (0 × 2²) + (1 × 2¹) + (1 × 2?) which is 11.

How to convert the binary value into a decimal value using PHP?

In this program, we are using the built-in function bindec() to convert binary to a decimal value. First, we have to accept the binary value from the user and assigning it to the variable bin and we have to create a variable flag with the value true and assign the length of the variable bin to the variable len. Then we have to perform for loop to check whether the entered num is a valid binary number or not first assign the value 0 into the variable i and perform the loop until the condition 'i < len' becomes false and also increment the value of the variable i in every iteration in the block of the loop we have to Check the condition 'bin[i] != 0' and 'bin[i] != 1' if both are true assign the value false to the variable flag and after the completion of the loop we have to Check the value of variable flag if it is true assign the value of built-in function 'bindec(bin)' to the variable dec and print the value of the variable des as the binary equivalent of the variable bin otherwise print the entered number is not a valid binary number

Syntax of function bindec()


bindec(binary_string);
 

ALGORITHM

Step 1: Accept the binary number into the variable bin

Step 2: Create a variable with the value true

Step 3: Assign the length of the variable bin to the variable len

Step 4: Perform the for loop to check whether the entered number is a valid binary number or not first assign the value 0 into the variable i and perform the following sub-steps until the condition 'i < len' becomes false and also increment the value of the variable i in every iteration

        (i) Check the condition 'bin[i] != 0' and 'bin[i] != 1' if both are true assign the value false to the variable flag

Step 5: Check the value of the variable flag if it is true assign the value of built-in function 'bindec(bin)' to the variable dec and print the value of the variable des as the binary equivalent of the variable bin otherwise print the entered number is not a valid binary number

PHP Source Code

                                          <?php
$bin = readline("Enter the binary value: ");
$flag = true;
$len = strlen($bin);
for ($i = 0; $i < $len; $i++) {
    if ($bin[$i] != 1 && $bin[$i] != 0) {
        $flag = false;
    }
}
if ($flag) {
    $dec = bindec($bin);
    echo "Decimal equivalent of $bin is $dec";
} else {
    echo "Enter a valid binary number.";
}
?>
                                      

OUTPUT

Enter the binary value: 100111
Decimal equivalent of 100111 is 39