|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-04-13 22:49 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 22 05:00:01 2025 UTC |
try printf("%u") though to print it - that would work...Yes, but echo will show the signed representatation. This is *not* a bug, use printf("%u") to show the unsigned version.It has nothing do with "echo showing the signed version." If that were the case, it would output the same (negative) number when you echo $before as well. It's PHP /using/ the signed version. For example: $test[$before] = 'It works!'; if (isset($test[$after])) { echo "It works!\n"; } else { echo "It doesn't work!\n"; } $before and $after are /not/ the same value to PHP. Nothing to do with echo.