Re: PHP & Hex Numbers

From: Date: Mon, 03 Jun 2002 13:27:15 +0000
Subject: Re: PHP & Hex Numbers
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-100392@lists.php.net to get a copy of this message
"Jonathan Rosenberg" <jr40@earthlink.net> wrote in message news:NDBBLKBKIKFIPIFMDDNHAEHGFPAA.jr40@earthlink.net... > would produce the same output. Is this correct? Theoretically... > > Either I'm missing something, or PHP gets very confused when > dealing with some numbers. For example, the following program > > <? > $result = 0x9E3779B9; > echo $result > printf('%d', $result); > ?> > > produces > > 2654435769 > -1640531527 > > I get the same kind of unexpected results when doing arithmetic > on such numbers. > > Is this tickling some kind of known bug in PHP? or am I > misunderstanding something? It's not a bug, just an 'inconsistency'. Both are correct... if you have a calculator that supports hex, type both those results in and convert to hex. Both should say 9E3779B9. The first result treats 9E3779B9 as an unsigned 32-bit value, the second result treats it as a _signed_ 32-bit value. If you use printf('%u', $result); (assuming PHP supports the full range of C printf() format specifiers), it should print out 2654435769 as does the straight echo. It seems echo treats numbers as unsigned... When doing arithmetic in code, you should always be prepared to handle signed and unsigned. -- -------------------------------------------- _ _ o o Jason Teagle < jason@teagster.co.uk v --------------------------------------------

« previous php.general (#100392) next »