int bindec ( string binary_string)
string decbin ( int number)
string dechex ( int number)
string decoct ( int number)
int hexdec ( string hex_string)
int octdec ( string octal_string)
string base_convert ( string number, int from_base, int to_base)
If you are unfamiliar with the term "base", it is the term used to define a given number system. In base b , we use the numbers 0, 1, ..., b -1, which means in base 10 we use the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, and in base 8 we use the numbers 0, 1, 2, 3, 4, 5, 6, 7. Given that base 10 uses the numbers 0 to 9, it should sound quite familiar to you - nearly everyone uses base 10 (decimal) for their calculations.
There are three other bases that have achieved particular popularity: base 2 (binary), base 8 (octal), and base 16 (hexadecimal). In base 2, we only have the numbers 0 and 1 to work with, and in base 16 we have the numbers 0-9, followed by A (10), B (11), C (12), D (13), E (14), and F (15).
To make sure you are clear, here's the number 39 in four different bases:
100111 (binary)
47 (octal)
39 (decimal)
27 (hexadecimal)
To convert hexadecimal to decimal, you multiply the 2 from the 27 by 16, then add 7, which gives 39. If the hexadecimal (often just called "hex") number was 2F8, you would multiply the 2 by 256 (256 = 16 * 16), the F by 16, and add the 8, which gives 760. If this sounds crazy to you, it is simply because you are so used to decimal - remember that in the number 569, our brain (invisibly) multiplies the 5 by 100 (100 = 10 * 10, of course), the 6 by 10, and adds the 9.
Converting from hexadecimal to binary, or octal to decimal might seem hard, but luckily PHP has several functions available to use that do the hard work: bindec(), decbin(), dechex(), decoct(), hexdec(), and octdec(). To understand what each does, simply split the function name in two - bindec() converts bin(ary) to dec(imal); dechex() converts dec(imal) to hex(adecimal).
Each function takes just one parameter, which is the number to convert to the new base. Here is a PHP example:
<?php
print decbin(16); // 10000
print dechex(232); // e8
print hexdec(e8); // 232
?>
So, converting from decimal to other bases, and from other bases to decimal is fairly easy. But what if you want to convert binary to hex? Well, naturally it is impractical for PHP to include separate functions to convert every base to every other base, so they simply provide one function: base_convert().
Base_convert() 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:
<?php
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 advantages 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).
One important thing to note is that numbers above 9 are represented using letters from the Latin alphabet. So in hex, we have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. In vigesimal (base 20), we have 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, G, H, I, and J. Given that there are only ten numbers from 0 to 9, and only 26 letters from A to Z, the maximum base we can handle is base 36. Technically it is possible to go to base 62 by differentiating between upper- and lowercase letters, but this is not supported by PHP at the current time - if you try to use a base larger than 36 you will get an error.
Author's Note: If you are interested, MIME uses base 64 by utilising ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 as well as + and /.
Here are a couple more examples of base_convert(), just to get you going:
<?php
print base_convert(556, 10, 2); // 1000101100
print base_convert(556, 10, 8); // 1054
print base_convert(556, 10, 20); // 17g
print base_convert(556, 10, 36); // fg
?>
As you can see, the larger our base, the shorter the eventual number.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Mathematical constants >>
Previous chapter: Other mathematical conversion functions
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.