base_convertRETURNS a STRING resulting from converting a number between arbitrary bases.
This function returns a STRING containing $num represented in base $to_base.
Both $from_base and $to_base should be between 2 and 36.
If both $from_base and $to_base greater than 10, they must be represented by letters a through z, where:
a = 10, b = 11, ..., z = 35.
The letters is interpreted case-insensitively.
This function may lose precision on large numbers due to properties related to the internal float type used.
<?php
string base_convert ( string $num, int $from_base, int $to_base )
where,
$num = The number to convert
$from_base = The original base of the number
$to_base = The final base to convert the number
?>
$num
The number to convert.
Any invalid characters are silently ignored.
As of PHP 7.4.0 supplying any invalid characters is deprecated.
$from_base
The original base of the number.
$to_base
The final base to convert the number.
lowercase |
UPPERCASE |
VALUE |
a |
A |
10 |
b |
B |
11 |
c |
C |
12 |
d |
D |
13 |
e |
E |
14 |
f |
F |
15 |
g |
G |
16 |
h |
H |
17 |
i |
I |
18 |
j |
J |
19 |
k |
K |
20 |
l |
L |
21 |
m |
M |
22 |
n |
N |
23 |
o |
O |
24 |
p |
P |
25 |
q |
Q |
26 |
r |
R |
27 |
s |
S |
28 |
t |
T |
29 |
u |
U |
30 |
v |
V |
31 |
x |
X |
32 |
w |
W |
33 |
y |
Y |
34 |
z |
Z |
35 |
ed48 |
EXERCISE
<?php
$dec01 = '42';
$frombase01 = 10;
$tobase01a = 2;
$tobase01b = 5;
$tobase01c = 8;
$tobase01d = 16;
$tobase01e = 36;
$dec2bin = base_convert($dec01, $frombase01, $tobase01a);
$dec2five = base_convert($dec01, $frombase01, $tobase01b);
$dec2oct = base_convert($dec01, $frombase01, $tobase01c);
$dec2hex = base_convert($dec01, $frombase01, $tobase01d);
$dec2thirtyfive = base_convert($dec01, $frombase01, $tobase01e);
echo 'base_convert("' . $dec01 . '", ' . $frombase01
. ', ' . $tobase01a . ') = (' . $dec2bin . ')<sub>'
. $tobase01a . '</sub><br><br><br>';
echo 'base_convert("' . $dec01 . '", ' . $frombase01
. ', ' . $tobase01b . ') = (' . $dec2five . ')<sub>'
. $tobase01b . '</sub><br><br><br>';
echo 'base_convert("' . $dec01 . '", ' . $frombase01
. ', ' . $tobase01c . ') = (' . $dec2oct . ')<sub>'
. $tobase01c . '</sub><br><br><br>';
echo 'base_convert("' . $dec01 . '", ' . $frombase01
. ', ' . $tobase01d . ') = (' . $dec2hex . ')<sub>'
. $tobase01d . '</sub><br><br><br>';
echo 'base_convert("' . $dec01 . '", ' . $frombase01
. ', ' . $tobase01e . ') = (' . $dec2thirtyfive . ')<sub>'
. $tobase01e . '</sub>';
?>
EXERCISE
<?php
// Run this code several times
$dec02 = mt_rand(2, 32000);
$frombase02 = 10;
$tobase02 = [ 2, 8, 16 ];
foreach($tobase02 as $base02)
{
$dec2base = base_convert($dec02, $frombase02, $base02);
echo 'base_convert("' . $dec02 . '", ' . $frombase02
. ', ' . $base02 . ') = (' . $dec2base . ')<sub>'
. $base02 . '</sub><br><br><br>';
}
?>
EXERCISE
<?php
$number03a = '3MUEOH8BI53LJ';
$frombase03a = 35;
$tobase03a = 10;
$base2dec03a = base_convert($number03a, $frombase03a, $tobase03a);
echo 'base_convert("' . $number03a . '", ' . $frombase03a
. ', ' . $tobase03a . ') = (' . $base2dec03a . ')<sub>'
. $tobase03a . '</sub><br><br><br>';
$number03ba = '3mueoh8bi53lj';
$frombase03ba = 35;
$tobase03ba = 10;
$base2dec03ba = base_convert($number03ba, $frombase03ba, $tobase03ba);
echo 'base_convert("' . $number03ba . '", ' . $frombase03ba
. ', ' . $tobase03ba . ') = (' . $base2dec03ba . ')<sub>'
. $tobase03ba . '</sub><br><br><br>';
?>
EXERCISE
<?php
$frombase = array(2,8,10,16,36);
$tobase = array(2,8,10,16,36);
$values = array(10,
27,
39,
03,
0x5F,
"10",
"27",
"39",
"5F",
"3XYZ"
);
for ($f= 0; $f < count($frombase); $f++) {
echo "<br>From base is ", $frombase[$f], "<br>";
for ($t= 0; $t < count($tobase); $t++) {
echo " to base is ", $tobase[$t], "<br>";
for ($i =0; $i < count($values); $i++){
$res = base_convert($values[$i],$frombase[$f],$tobase[$t]);
echo " value= ",
$values[$i], " res = ", $res, "<br>";
}
}
}
?>
EXERCISE
<?php
echo " Testing base_convert() : error conditions.";
// get a class
class classA
{
}
try {
base_convert(1234, 1, 10);
} catch (ValueError $e) {
echo $e->getMessage(), "<br><br>";
}
try {
base_convert(1234, 10, 37);
} catch (ValueError $e) {
echo $e->getMessage(), "<br><br>";
}
try {
base_convert(new classA(), 8, 10);
} catch (Error $e) {
echo $e->getMessage(), "<br><br>";
}
?>
EXERCISE
<?php
echo 'base_convert(\'FF\', 16, 10) = '. base_convert('FF', 16, 10) . "<br><br>";
echo 'base_convert(\'20\', 8, 10) = ' . base_convert('20', 8, 10) . "<br><br>";
echo 'base_convert(\'0xFf\', 16, 10) = ' . base_convert('0xFf', 16, 10) . "<br><br>";
echo 'base_convert(\'0FF\', 16, 10) = ' . base_convert('0FF', 16, 10) . "<br><br>";
echo 'base_convert(" 0xFF\t\n" , 16, 10) = ' . base_convert(" 0xFF\t\n" , 16, 10) . "<br><br>";
echo 'base_convert(" 0xFF\t\n" , 16, 10) = ' . base_convert(" 0xFF\t\n" , 16, 10) . "<br><br>";
echo 'base_convert("\r\nFF\t\n" , 16, 10) = ' . base_convert("\r\nFF\t\n" , 16, 10) . "<br><br>";
echo 'base_convert("0o7" , 8, 10) = ' . base_convert("0o7" , 8, 10) . "<br><br>";
echo 'base_convert("0b1010" , 2, 10) = ' . base_convert("0b1010" , 2, 10) . "<br><br>";
echo "<br><br>";
echo 'THESE SHOULD FAIL!<br><br>';
echo 'base_convert(\'fg\', 16, 10) = ' . base_convert('fg', 16, 10) . "<br><br>";
echo "base_convert('f 0xff ', 16, 10) = " . base_convert('f 0xff ', 16, 10) . "<br><br>";
echo "base_convert('1xff ', 16, 10) = " . base_convert('1xff ', 16, 10) . "<br><br>";
echo 'base_convert(chr(0), 16, 10) = ' . base_convert(chr(0), 16, 10) . "<br><br>";
echo 'base_convert("0o7" , 9, 10) = ' . base_convert("0o7" , 9, 10) . "<br><br>";
echo 'base_convert("0 0" , 9, 10) = ' . base_convert("0 0" , 9, 10) . "<br><br>";
?>
EXERCISE
<?php
echo 'PHP ' . PHP_VERSION . '<br><br>';
echo "Testing base_convert() : usage variations.";
//get an unset variable
$unset_var = 10;
unset ($unset_var);
// heredoc string
$heredoc = <<<EOT
abc
xyz
EOT;
// get a resource variable
$fp = fopen(__FILE__, "r");
$inputs = array(
// int data
/*1*/ 0,
1,
12,
-12,
2147483647,
// float data
/*6*/ 10.5,
-10.5,
1.234567e2,
1.234567E-2,
.5,
// null data
/*11*/ NULL,
null,
// boolean data
/*13*/ true,
false,
TRUE,
FALSE,
// empty data
/*17*/ "",
'',
array(),
// string data
/*20*/ "abcxyz",
'abcxyz',
$heredoc,
// undefined data
/*23*/ @$undefined_var,
// unset data
/*24*/ @$unset_var,
// resource variable
/*25*/ $fp
);
// loop through each element of
// $inputs to check the behaviour of base_convert()
$iterator = 1;
foreach($inputs as $input) {
echo "<br><br>Iteration: $iterator<br>";
try {
var_dump(base_convert($input, 10, 8));
} catch (TypeError $exception) {
echo $exception->getMessage() . "<br><br>";
}
$iterator++;
}
fclose($fp);
?>