Home >Backend Development >PHP Tutorial >php base_convert() base number conversion function_PHP tutorial
The base_convert() function converts numbers between arbitrary bases.
Grammar
base_convert(number,frombase,tobase) parameter description
number required. original value.
frombase required. The original base of the number.
tobase is required. The base to be converted.
*/
$hexadecimal='a37334';
echo base_convert($hexadecimal,16,2); //Convert to binary output 101000110111001100110100
echo "
";
$number="123";
echo base_convert($number,10,2); //Convert to binary output 1111011
echo "
";
echo base_convert($number,10,8); //Convert to octal output 173
echo "
";
echo base_convert($number,10,16); //Convert to hexadecimal output 7b
$number2="100000101";
echo "
";
echo base_convert($number2,2,10); //Convert to decimal output 261
echo "
";
echo base_convert($number2,2,8); //Convert to octal output 405
/*
Description
Returns a string containing the tobase representation of number. The base of number itself is specified by frombase. Both frombase and tobase can only be between 2 and 36 (inclusive). Numbers above decimal are represented by the letters a-z, such as a for 10, b for 11, and z for 35.