ID:               35817
 User updated by:  lynjwxm at hotmail dot com
 Reported By:      lynjwxm at hotmail dot com
 Status:           Open
 Bug Type:         Strings related
 Operating System: rhel as 3
 PHP Version:      5.1.1
 New Comment:

and it seems php has the diffrent behavior with perl's:

perl's behavior for 3 bytes(odd bytes)
$out=pack "H3","181";
$out1=unpack "H3",$out;
print $out1;
---------------
expect result:
181
---------------
actual result:
181

perl's behavior for one byte(odd bytes)
$out=pack "H","8";
$out1=unpack "H",$out;
print $out1;
---------------
expect result:
8
---------------
actual result:
8

perl's behavior for 4 byte(even bytes)
$out=pack "H4","1818";
$out1=unpack "H4",$out;
print $out1;
---------------
expect result:
1818
---------------
actual result:
1818

So, can I say it is a bug?


Previous Comments:
------------------------------------------------------------------------

[2005-12-28 04:12:44] lynjwxm at hotmail dot com

simplest:
<?php
$a=pack("H","8");
var_dump($a);
var_dump(bin2hex($a));
$b=unpack("H",$a);
var_dump($b);
?>

---------------
string(1) "   "
string(2) "80"
array(1) {
  [1]=>
  string(0) ""
}
---------------
Do you mean I can not pack hex string "8"? I should only pack hex
string "88" or so?

------------------------------------------------------------------------

[2005-12-28 03:51:26] lynjwxm at hotmail dot com

Do you mean we can only pack hex string in even bytes?

------------------------------------------------------------------------

[2005-12-28 02:30:28] lynjwxm at hotmail dot com

in source code:
in func: PHP_FUNCTION(pack)
                switch ((int) code) {
                        case 'h': 
                        case 'H': 
                                INC_OUTPUTPOS((arg + 1) / 2,1)  /* 4 bit per 
arg */
                                break;

in func: PHP_FUNCTION(unpack)
                        case 'h': 
                        case 'H':       
                                size = (arg > 0) ? arg / 2 : arg; 
                                arg = 1;
                                break;                  

we can find that when we pack an odd number bytes hexstring in function
pack(),it add an extra byte of '0'.But it did not do so in function
unpack().

<?php
$a = pack("H3i", "1810", 513);
var_dump(bin2hex($a));
var_dump(unpack("H3/iabc", $a));
?>

before change code, result:
string(12) "181001020000"
array(2) {
  [1]=>
  string(2) "18"
  ["abc"]=>
  int(131344)
}


--                              size = (arg > 0) ? arg / 2 : arg; 
++                              size = (arg > 0) ? (arg + 1) / 2 : arg;

after change code, result:
string(12) "181001020000"
array(2) {
  [1]=>
  string(4) "1810"
  ["abc"]=>
  int(513)
}

------------------------------------------------------------------------

[2005-12-27 14:56:41] [EMAIL PROTECTED]

Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.



------------------------------------------------------------------------

[2005-12-27 10:40:49] lynjwxm at hotmail dot com

Description:
------------
(sorry for my poor english.....)
It seems unpack can not deal with an odd number of hex string args.
Under a xterm, it will cause "xterm" displayed on term. 

Reproduce code:
---------------
<?php
$a=pack("H3i","181",5);
$b=unpack("H3/ias",$a);
var_dump($b);
?>

Expected result:
----------------
array(2){
[1]=>
string(3)"181"
["as"]=>
int(5)
}

Actual result:
--------------
array(2){
[1]=>
string(2)"18"
["as"]=>
int(1296)
}


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=35817&edit=1

Reply via email to