Hudzilla.org - the homepage of Paul Hudson
Contents > Simple variables and operators Wish List | Report Bug | About Me ]

3.5     Non-decimal number systems

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

Although most numbers in PHP are entered in using decimal (base 10), you may also specify them in hexadecimal (base 16) or octal (base 8). Of the two, the first is rare, and the second is even rarer, but it is important that you are aware of both in case you stumble across it one day.

As you know already, decimal numbers are specified using the digits 0 to 9, which makes 3291 the number three thousand two hundred and ninety-one. That same number in base 8, which uses just the digits 0 to 7, is 6333. In hexadecimal, which uses 0 to 9 then A, B, C, D, E, and F, the number is "CDB". However, if you specify the number in code like this, it will not work as expected:

$octalnum = 6333;

The reason for this is because PHP will read it as decimal 6333, which is actually octal 14,275! To specify that a number is written in octal as opposed to decimal, you must precede it with a 0. So, to say that you mean $octalnum to be set to octal 6333 (decimal 3291), you would use this code:

<?php
    $octalnum
= 06333;
    print
$octalnum;
?>

That script outputs 3291, as PHP always works with decimal internally, and converts octal 06333 to decimal 3291 when $octalnum is set.

To specify you are providing a number in hexadecimal, you need to precede it with 0x. For example, to use the number 68, you would use this:

<?php
    $hexnum
= 0x44;
    print
$hexnum;
?>

Again, note that the value is printed out in standard decimal. Octal notation is very rarely used in PHP. If you are on Unix, you may have to use it to specify permissions, but that's generally the only use. Hexadecimal notation ("hex") is more common, mostly because many hashing algorithms return text using hexadecimal characters, and also because HTML's colour codes are written in hex.





<< 3.4 Forcing a type with type casting   3.6 Variable scope >>
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
amol_bhavsar1982@yahoo.co.in - 29 Aug 2008

I am Amol,I live i Pune.

Sad Git Me? - 29 Aug 2008

I once had to write a postcript bitmap print program that required the pixels to be converted to hex (and lots of them), never used it since.... Useful to know, but not common to use anything other than base10 for output. But then Fortran77 LOL ;-)

Paul

A Fortran77 User ;o) - 29 Aug 2008

A little bit of Googling helps, or just read the PHP manual.

See here for a complete list of formatting information: http://www.php.net/manual/en/function.sprintf.php

Where echo and print are internal php commands, printf and sprintf are the C-library calls with the same name.

So if you want to print a hexadecimal number, you can do this:

<?php

$myvar = 0x0A;

printf( "Decimal : %d\n", $myvar );
printf( "Octal : %o\n", $myvar );
printf( "Hexadecimal: %x\n", $myvar );

?>

This will output:

Decimal : 10
Octal : 12
Hexadecimal: a

Cheers,
Luud Heck

Kyle - 29 Aug 2008

Okay, so what do we need to do to print out a number in either octal or hex? I would hope there is a way, but what is it?

A PHP User - 29 Aug 2008

This may be clearer:

To specify that a number is written in octal as opposed to decimal, you must precede it with a 0 (the number zero).



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 one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow