Re: Can someone explain this?
| From: | René Fournier | Date: | Tue, 08 Jun 2004 17:59:54 +0000 |
| Subject: | Re: Can someone explain this? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-187902@lists.php.net to get a copy of this message | ||
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?
...Rene
On Tuesday, June 8, 2004, at 11:29 AM, Curt Zirzow wrote:
* Thus wrote Ren Fournier (m5@renefournier.com):-= PRODUCES: =- -333371788 3961595508 Is this something about signed versus unsigned integers? What I really would like to do is convert that negative number (-333371788), which I suppose is unsigned to a signed integer (3961595508) without having to convert it to hex, then back to decimal.You have it backwords.. -333371788 is signed and the 3961595508 is unsigned. But... <?php $dec = -333371788; $unsigned = sprintf("%ul", $dec); print $unsigned; // == 3961595508 Do note that php doesn't have native unsigned numbers so doing something like this wont work right: printf("%d", $unsigned); // == 2147483647 So to convert it back you have to do something like: printf("%d", $unsigned+0); // == -333371788 Curt -- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid! -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php