php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39588 unpack("a*", "abc\0") does not work as expected
Submitted: 2006-11-22 14:09 UTC Modified: 2006-11-22 14:23 UTC
From: pprasse at actindo dot de Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.4.4 OS: linux 2.6.10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pprasse at actindo dot de
New email:
PHP Version: OS:

 

 [2006-11-22 14:09 UTC] pprasse at actindo dot de
Description:
------------
unpack( "a*", $string ) does not work as expected when last character of $string is "\0"


Reproduce code:
---------------
<?php

$string = "abc\0";
$arr = unpack( "a*buf", $string );
printf( "strlen = %d\n", strlen($arr['buf']) );
printf( "hex dump:\t" );
for( $i=0; $i<strlen($arr['buf']); $i++ )
  printf( " %02x", ord($arr['buf']{$i}) );

printf( "\n" );

?>

Expected result:
----------------
strlen = 4
hex dump:	 61 62 63 00


Actual result:
--------------
strlen = 3
hex dump:	 61 62 63


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-11-22 14:23 UTC] tony2001@php.net
Expected behaviour.

$var = "str";
$packed = pack("a5", $var); // 5 bytes long NULL-padded string
var_dump(unpack("a*", $packed)); // the NULL-padding is removed to get the original data.
 [2012-02-10 09:11 UTC] su dot hang at yahoo dot com
is this also expected behavior?

<?php
$var = "str";
$packed = pack("a5", $var); // 5 bytes long NULL-padded string
var_dump(unpack("a5", $packed));

array(1) {
  [1]=>
  string(3) "str"
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 13:01:30 2024 UTC