base_convert()
As we’ve just explored, PHP has some built in functions for converting numbers from or to binary, hex, or octal with respect to decimal numbers. This function, base_convert(), will convert a number from any base to any base.
That’s not entirely true. It only works with bases 2-36 inclusive, but that should be enough for the common stuff.
$bin = '10100010'; $oct = '624'; $dec = '1985'; $hex = 'C477DF'; $res = base_convert($bin, 2, 10); // 162 $res = base_convert($bin, 2, 16); // a2 $res = base_convert($oct, 8, 10); // 404 $res = base_convert($oct, 8, 2); // 110010100 $res = base_convert($dec, 10, 2); // 11111000001 $res = base_convert($dec, 10, 16); // 7c1 $res = base_convert($hex, 16, 10); // 12875743 $res = base_convert($hex, 16, 8); // 61073737 $res = base_convert('17', 22, 3); // 1002 $res = base_convert('OU812', 36, 12); // 11b80032
