Re: Fw: unpack()
| From: | Dmitry Stogov | Date: | Wed, 02 Mar 2016 12:59:49 +0000 |
| Subject: | Re: Fw: unpack() | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-91475@lists.php.net to get a copy of this message | ||
I've added a test to explain the behavior https://gist.github.com/dstogov/edbb1efcd1e9701e75ab
"@" and $offset might be used together and work additive.
Thanks. Dmitry.
________________________________________
From: Leigh <leight@gmail.com>
Sent: Thursday, February 25, 2016 18:20
To: Dmitry Stogov
Cc: Nikita Popov; internals@lists.php.net; Hynek Bartoš
Subject: Re: [PHP-DEV] Fw: unpack()
On 25 February 2016 at 13:02, Dmitry Stogov <dmitry@zend.com> wrote:
>
> I've just got to know about possible usage of "@" in unpack(), but it seems
> doesn't work at all
>
> $ sapi/cli/php -r 'var_dump(unpack("@0l", "\x01\x00\x00\x00")); '
> array(0) {
> }
>
> Do I use it properly?
>
> Thanks. Dmitry.
>
As Nikita said, you need the "/". I use this operator when processing
streams of binary data.
Trimmed down example:
while ($blocks--) {
list(, $x0, $x1, $x2, $x3) = unpack("@$offset/N4", $message);
//...
$offset += 16;
}
The dynamic format string is kind of nasty though, so no objections to
an additional parameter making things clearer, however, what is to be
done if an offset is specified in both the format string and the
additional parameter? Does one override the other? Are they additive?