Converting Between Bases

You need to convert a number from one base to another.

Use base_convert() function. It changes a string in one base to the correct string in another.

there are also a few specialized functions for conversion to and from base 10 and other bases of:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>Converting Between Bases</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php

echo "<p><b>Convert from base 16 to base 10.</b></p>";
echo "hex 1b to decimal: ", base_convert('1b', 16, 10), "<br />";

echo "<p><b>Convert to base 10.</b></p>";

echo "binary 11011 to decimal: ", bindec(11011), "<br />";
echo "octal 33 to decimal: ", octdec(33), "<br />";
echo "hex 1b to decimal: ", hexdec('1b'), "<br />";

echo "<p><b>Convert from base 10.</b></p>";

echo "decimal 27 to binary: ", decbin(27), "<br />";
echo "decimal 27 to octal: ", decoct(27), "<br />";
echo "decimal 27 to hex: ", dechex(27), "<br />";

?>
</p>
</body>
</html>

View the effect


Numbers | Introduction | Checking a Valid Number | Comparing FLoating-Point Numbers | Rounding FLoating-Point Numbers | Series of Integers | Random Numbers | Logarithms | Exponents | Formatting Numbers | Formatting Monetary Values | Printing Correct Plurals | Trigonometric Functions | Converting Between Bases
© 2008: Hann So
email: hso@voyager.deanza.edu