The term base-conversion means the conversion of the number system. In this program, we use the built-in function base_convert()
to convert the type of the number. In the function base_convert() we pass three arguments first is the value to be converted second is the base value of the number to be converted from and at last we pass the base value of the type we convert to. For example, if we enter a decimal value 26 and we want to convert it to hexadecimal then the output will be 1A
Syntax of function base_convert()
base_convert(number,frombase,tobase);
In this program, we are converting a decimal number into binary, hexadecimal, and octal. For that first, we have to accept the decimal number from the user and assign the value to the variable num. Then to convert decimal to binary we use the function base_convert(num, 10,2) and the result will be assigned to the variable bin, To convert decimal to hexadecimal we use the function base_convert(num, 10,16) and the result will be assigned to the variable hexa and at last To convert decimal to octal we use the function base_convert(num, 10,8) and the result will be assigned to the variable octa. After that, we can print the values of the variable bin as the binary equivalent, hexa as the hexadecimal equivalent, and octa as the octal equivalent of the decimal number entered.
Step 1: Accept the decimal number from the user and assign the value to the variable num
Step 2: To convert decimal to binary we use the function base_convert(num, 10,2) and the result will be assigned to the variable bin
Step 3: To convert decimal to hexadecimal we use the function base_convert(num, 10,16) and the result will be assigned to the variable hexa
Step 4: To convert decimal to octal we use the function base_convert(num, 10,8) and the result will be assigned to the variable octa
Step 5: Print the value in the variable bin as the binary equivalent of the decimal number entered
Step 6: Print the value in the variable hexa as the hexadecimal equivalent of the decimal number entered
Step 7: Print the value in the variable octa as the octal equivalent of the decimal number entered
<?php
$num = readline("Enter the decimal value: ");
$bin = base_convert($num, 10, 2);
$hexa = base_convert($num, 10, 16);
$octa = base_convert($num, 10, 8);
echo "Binaray equivalent of $num is $bin \n";
echo "Hexadecimal equivalent of $num is $hexa \n";
echo "Octal equivalent of $num is $octa \n";
?>
Enter the decimal value: 58 Binaray equivalent of 58 is 111010 Hexadecimal equivalent of 58 is 3a Octal equivalent of 58 is 72