|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36148 unpack("H*hex", $data) is adding an extra character to the end of the string.
Submitted: 2006-01-25 00:09 UTC Modified: 2006-01-26 17:01 UTC
From: andy at advancethermal dot com Assigned: iliaa (profile)
Status: Closed Package: Strings related
PHP Version: 5CVS-2006-01-25 (snap) OS: *
Private report: No CVE-ID: None
 [2006-01-25 00:09 UTC] andy at advancethermal dot com
unpack("H*hex", $data) is adding an unexpected extra character to the end of the string. Upgraded to PHP 5.1.2 using Windows Zip package.

Reproduce code:
echo "<p>".unpack("H*hex", file_get_contents("c:/php/php.exe"));

Try in 5.0.5 and 5.1.2 -- there is a difference.

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-25 00:10 UTC] andy at advancethermal dot com
Sorry, reverse the Expected and Actual Results...
 [2006-01-25 00:32 UTC]
See bug #35817.
 [2006-01-25 18:22 UTC] andy at advancethermal dot com
Upon further testing, I found the following:

 $values = array("a", "aa", "aaa", "aaaa");
 foreach ($values as $value) {
   $a = pack("H*", $value);
   $b = unpack("H*", $a);
   echo "<p>".$value.": ";

a: array(1) { [1]=> string(3) "a00" }
aa: array(1) { [1]=> string(3) "aa0" }
aaa: array(1) { [1]=> string(5) "aaa00" }
aaaa: array(1) { [1]=> string(5) "aaaa0" }

EXPECTED RESULT: (produced from PHP 5.0.5)
a: array(1) { [1]=> string(2) "a0" }
aa: array(1) { [1]=> string(2) "aa" }
aaa: array(1) { [1]=> string(4) "aaa0" }
aaaa: array(1) { [1]=> string(4) "aaaa" }

The release notes for 5.1.2 indicate that the problem was fixed, however this does not appear to be the case -- or is a new problem.  All scripts that handle binary data using unpack() had to be modified with substr($unpackeddata[1], 0, -1) so it would be properly handled.
 [2006-01-25 18:53 UTC] andy at advancethermal dot com
The latest CVS (5.1.3-dev built Jan 25 2006 16:19:11) does not correct the problem.
 [2006-01-25 18:59 UTC]
Ilia, verified on Linux too.
 [2006-01-26 17:01 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Fri Jun 02 23:03:39 2023 UTC