Converts a number from one base to another. The base the number is currently in is from, and the base to convert to is to. The bases to convert from and to must be between 2 and 36. Digits in a base higher than 10 are represented with the letters a (10) through z (35). Up to a 32-bit number, or 2,147,483,647 decimal, can be converted.
base_convert(string $num, int $from_base, int $to_base): string
It is impractical for PHP to include separate functions to convert every base to every other base, so they are grouped into one function: base_convert(). This takes three parameters: a number to convert, the base to convert from, and the base to convert to. For example, the following two lines are identical:
print decbin(16); print base_convert("16", 10, 2);
The latter is just a more verbose way of saying “convert the number 16 from base 10 to base 2.” The advantage of using base_convert() is that we can now convert binary directly to hexadecimal, or even crazier combinations, such as octal to duodecimal (base 12) or hexadecimal to vigesimal (base 20).
Highest Base Supported
The highest base that base_convert() supports is base 36, which uses 0-9 and then A-Z. If you try to use a base larger than 36, you will get an error.
|num||The number to convert. Any invalid characters in num are silently ignored.|
|from_base||The base num is in|
|to_base||The base to convert num to|
num converted to base to_base.
Example 1: Convert a number’s base:
$org = "564"; $new = base_convert($org, 8, 16); echo "$org in octal is the same as $new in hexadecimal\n";
$hexadecimal = 'a37334'; echo base_convert($hexadecimal, 16, 2);