|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-08-29 10:22 UTC] hiroaki dot kawai at gmail dot com
Description:
------------
According to the documentation, all I, L, N and V formats are defined to be
unsigned integer. On my Linux(32bit intel), unpacking FFFFFF returns -1. As you
can see the test script, we automatically use float for large integer. So the
result should be float(4294967295), otherwise we get wrong number of -1.
Test script:
---------------
<?php
var_dump(unpack("I","\xFF\xFF\xFF\xFF"));
var_dump(unpack("L","\xFF\xFF\xFF\xFF"));
var_dump(unpack("N","\xFF\xFF\xFF\xFF"));
var_dump(unpack("V","\xFF\xFF\xFF\xFF"));
var_dump(PHP_INT_MAX);
var_dump(PHP_INT_SIZE);
var_dump(4294967295);
Expected result:
----------------
array(1) {
[1]=>
float(4294967295)
}
array(1) {
[1]=>
float(4294967295)
}
array(1) {
[1]=>
float(4294967295)
}
array(1) {
[1]=>
float(4294967295)
}
int(2147483647)
int(4)
float(4294967295)
Actual result:
--------------
array(1) {
[1]=>
int(-1)
}
array(1) {
[1]=>
int(-1)
}
array(1) {
[1]=>
int(-1)
}
array(1) {
[1]=>
int(-1)
}
int(2147483647)
int(4)
float(4294967295)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Dec 12 06:00:01 2025 UTC |
> unpack/pack wouldn't be inverse operations anymore. Yes it does inverse operations. Let's see the result below. -------- code <?php var_dump(bin2hex(pack("I",4294967295))); -------- result string(8) "ffffffff" pack() works fine with float(4294967295). Please note that "float" is the PHP internal representation, users should use numbers what they want.That's because when you do pack("I",4294967295) the float(4294967295) is cast into an int before the conversion. Notice (x86): $ php -r 'var_dump((int)4294967295);' int(-1) So you're actually packing int(-1) as unsigned. It seems reasonable that you receive a int(-1) back when you unpack it. Returning a float has several problems: performance, bitwise operators may not work as expected etc. Like I said, the signed/unsigned is only relevant when you may have to move the sign bit.