base_convert
Description
The base_convert of Math for PHP convert a number between arbitrary bases.
Syntax
base_convert ( string $number , int $frombase , int $tobase ) : string
Parameters
number
The number to convert. Any invalid characters in number are silently ignored.
frombase
The base number is in
tobase
The base to convert number to
Return
number converted to base tobase
Examples
1
<? $number = 16; $frombase = 10; $tobase = 2; $return = base_convert($number, $frombase, $tobase); echo $return;
10000
2
<? for($i = 0; $i < 16; ++$i) { $number = $i; $frombase = 10; $tobase = 2; $return = base_convert($number, $frombase, $tobase); echo $number . ":" . $return . PHP_EOL; }
0:0 1:1 2:10 3:11 4:100 5:101 6:110 7:111 8:1000 9:1001 10:1010 11:1011 12:1100 13:1101 14:1110 15:1111
3
<? for($i = 0; $i < 16; ++$i) { $number = $i; $frombase = 10; $tobase = 8; $return = base_convert($number, $frombase, $tobase); echo $number . ":" . $return . PHP_EOL; }
0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7 8:10 9:11 10:12 11:13 12:14 13:15 14:16 15:17
4
<? for($i = 0; $i < 16; ++$i) { $number = $i; $frombase = 10; $tobase = 16; $return = base_convert($number, $frombase, $tobase); echo $number . ":" . $return . PHP_EOL; }
0:0 1:1 2:2 3:3 4:4 5:5 6:6 7:7 8:8 9:9 10:a 11:b 12:c 13:d 14:e 15:f