On 08 June 2004 19:00, René Fournier wrote:

> OK, that makes sense. But here's the problem: I receive binary data
> from SuperSPARC (big-endian), which I need to unpack according to
> certain documented type definitions. For example, let's say that $msg
> has the value "3961595508" and is packed as an unsigned long integer
> (on the remote SPARC). But when I receive it, and unpack it...
> 
> $unpacked = unpack('Nval', $msg); // N for unsigned long integer,
> big-endian (SPARC) echo $unpacked["val"];
> 
> ...the output value is "-333371788". (???) Which tells me that PHP is
> NOT unpacking $msg as an unsigned long integer, but rather as
> a signed
> integer (since unsigned integers cannot be negative).
> 
> Now, thanks to your suggestions, I can convert that number back to an
> unsigned integer-or at least make it positive. But I
> shouldn't have to
> convert it, should I?

Yes.

Whether an integer is signed or unsigned is simply a matter of how you interpret the 
32 bits representing it -- unsigned 3961595508 is represented in 32 bits in exactly 
the same way as signed -333371788.  This explains the results you are getting: PHP 
*is* unpacking your binary(?) data as unsigned, but, as PHP doesn't have an unsigned 
type, the only place it has to put the resulting 32-bit representation is in a PHP 
integer, which is signed -- so when you print it, you get the signed representation.

To get PHP to print the unsigned representation of an integer, you can use the %u 
format specifier of sprintf() (http://www.php.net/sprintf) or one of its *printf 
friends.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to