Paul M Foster wrote:
>Anyone know how to pull data out of a file and interpret it as binary in
>PHP? Specifically, I have four bytes in a file whose contents I want to
>bit-shift left by varying amounts, and then store the results as a
>number. The raw data is binary. In python, it looks like this:
>
>s0 = ord(four_byte_string[0])
>s1 = ord(four_byte_string[1])
>s2 = ord(four_byte_string[2])
>s3 = ord(four_byte_string[3])
>return (s3 << 24) | (s2 << 16) | (s1 << 8) | s0
>
>and the value returned should be a number. Anyone know how to accomplish
>a similar thing in PHP?
>
>
Depending on the binary endianness, you could always just fread() in the
4 bytes into a string and settype() it to an integer (or do a "+= 0" to
convert it).
If you read in each byte, a character at a time, you can always use
pack() to ensure the endianness.
This is the kind of thing pack() and unpack() are for (vaguely similar
to the perl equivalent functions):
$handle = fopen("/some/path/binary_file.bin", "rb");
$s0 = fread($handle, 1);
$s1 = fread($handle, 1);
$s2 = fread($handle, 1);
$s3 = fread($handle, 1);
$binary_string = pack ('C4', $s0, $s1, $s2, $s3);
// This is just an example of how to use unpack()
$value=unpack('Linteger',$binary_string);
$integer=$value['integer'];
$integer+=0; // $integer is now a number containing the unpacked
Long (32bit) value parsed from $binary_string
// Alternatively, $binary_string is already a 32bit integer, just
make sure php treats it as a number. This _should_ work.
$binary_string+=0;
fclose($handle);
I think this is correct, but I've not written php in a while, and the
the above was pasted together off of some google searches without
testing any of it.
- Ian C. Blenke <icblenke@nks.net>
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.
This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:32:54 EDT