#32700 [Bgs->Opn]: unpack() returns signed values when it shouldn't

From: Date: Wed, 13 Apr 2005 21:01:56 +0000
Subject: #32700 [Bgs->Opn]: unpack() returns signed values when it shouldn't
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-77487@lists.php.net to get a copy of this message
ID: 32700 User updated by: ieure@php.net Reported By: ieure@php.net -Status: Bogus +Status: Open Bug Type: Unknown/Other Function Operating System: Linux PHP Version: 4.3.10 New Comment: While PHP may only support signed integers, the manual for pack() clearly states, regarding the 'N' format: "unsigned long (always 32 bit, big endian byte order)" But it returns a signed int when using this format, contrary to the documentation. There's obviously some sort of internal PHP state regarding the signedness of a particular value, since it's capable of correctly representing the unsigned value. It just seems that unpack() is doing something wrong here. Previous Comments: ------------------------------------------------------------------------ [2005-04-13 22:54:41] derick@php.net try printf("%u") though to print it - that would work... ------------------------------------------------------------------------ [2005-04-13 22:54:12] derick@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php PHP only supports signed integers. ------------------------------------------------------------------------ [2005-04-13 22:49:33] ieure@php.net Description: ------------ unpack() returns signed long values when the high bit is set, even though the unpack format is set to unsigned long. Workaround: $foo = unpack('N', $data); $foo = $foo << 1 >> 1; $foo += 0x80000000; Reproduce code: --------------- // High bit is set. $before = 0x80000002; echo "Before: $before\n"; // Outputs '2147483650' $int = pack('N', $before); $res = unpack('N', $int); $after = $res[1]; echo "After: $after\n"; // Outputs '-2147483646' Expected result: ---------------- $before and $after should be the same number. Actual result: -------------- $after is treated as signed, and does not equal $before. Script output: Before: 2147483650 After: -2147483646 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=32700&edit=1

« previous php.bugs (#77487) next »