Hudzilla.org - the homepage of Paul Hudson
Contents > Functions > Mathematics Wish List | Report Bug | About Me ]

4.6.5     Base conversion: bindec(), decbin(), dechex(), decoct(), hexdec(), octdec(), and base_convert()

This is NOT the latest copy of this book; click here for the latest version.

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.





<< 4.6.4 Other mathematical conversion functions: abs(), sqrt(), pow, and hypot()   4.6.6 Mathematical constants >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
fxtrader@yahoo.com - 06 Sep 2008

http://www.gheymatha.com
ÂãæÒÔ ÝÇÑÓ
ÊÍá&#1740;á Êä&#1740;Çá

A PHP User - 06 Sep 2008

Thanks a lot
http://www.roro44.com

trt@hgjl.yt - 06 Sep 2008

<div title="ÃÃÃÃÃ"><A href="http://www.forex.co.ir/" title="ÃÃÃÃÃ"><IMG alt="ÃÃÃÃà ÃÃæÃà ÃæÃÃÃà ÃíÂäÃá ÃÃÃÂÃÃ" src="http://www.forex.co.ir/forex.gif"></A> <A href="http://www.meta-fx.com/" title="ÃÃÃÃÃ"><IMG alt="ÃÃÃÃà ÃÃæÃà ÃæÃÃÃà ÃíÂäÃá ÃÃÃÂÃÃ" src="http://www.meta-fx.com/forex.gif"></A></div>
http://www.forex.co.ir/
http://www.meta-fx.com/ ÙØ§Ø±Ùس

Tom - 06 Sep 2008

This chapter was really helpful. I've been developing an encryption script that hammers numbers with various operations and conversions, and these last few pages were really helpful. Thank you.



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow