Markus Fischer wrote:
> Hi,
> 
> I'm searching for an efficient way to use unpack on binary data within a
> variable (php 5.1.something). In the past I was doing
> 
> fopen
> fseek
> unpack( fread(...)
> fseek
> etc.
> 
> Now this time I need to do the same, but I don't have an actual file: my
> data is only in a variable and I don't want to operate on a file but the
> variable within.
> 
> I was thinking about using substr() and passing the small chunks of data
> I need to the unpack() function.
> 
> Question: does this work without problems on arbitrary binary, maybe
> containing zero-byte values reliable?

Hi Markus,

This does work fine, but beware of strange behavior with unpack() when
using unsigned types V, N, or L.  I recently was bitten by different
behavior on 32-bit and 64-bit OSes unpacking a large unsigned integer.
If you need unsigned, use sprintf("%u", $whatever) to get the actual
integer.

substr() works nicely on strings for unpack(), see how PHP_Archive uses
it in http://cvs.php.net/viewvc.cgi/pear/PHP_Archive/Archive.php .  If
you are writing for forward compatibility with PHP 6's unicode and plan
to accept input from a variety of sources, you may want to cast the
string to binary type.

Greg

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to