Re: pack it in
| From: | Nathan Nobbe | Date: | Fri, 25 Jan 2008 04:20:27 +0000 |
| Subject: | Re: pack it in | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-268069@lists.php.net to get a copy of this message | ||
On Jan 24, 2008 7:13 PM, Jochem Maas <jochem@iamjochem.com> wrote:
> ok. that's where my brain goes to mush - all strings in php (for now)
> are binary ... what's the difference between the binary strings
> in php and such found in these 'binary files' that they need to be
> packed/unpacked?
>
> sorry if I'm sounding retarded ... I probably am :-/
>
pack() allows you to format binary data. it can be formatted for specific
architectures as well. formating of php stings does not take place on a
binary level, rather it occurs on a byte level, or multibyte using the
multibyte string functions
http://www.php.net/manual/en/ref.mbstring.php
here is a nice example i found from phpdig.net, that is designed to
determine whether a system is little, big, or middle endian. (there is a
small error on the script at the actual website; ive modified it for the
post
here and sent a notice to the site regarding it).
id like to see how this could be done w/ regular php strings :)
<?php
# A hex number that may represent 'abyz'
$abyz = 0x6162797A;
# Convert $abyz to a binary string containing 32 bits
# Do the conversion the way that the system architecture wants to
switch (pack ('L', $abyz)) {
# Compare the value to the same value converted in a Little-Endian
fashion
case pack ('V', $abyz):
echo 'Your system is Little-Endian.';
break;
# Compare the value to the same value converted in a Big-Endian fashion
case pack ('N', $abyz):
echo 'Your system is Big-Endian.';
break;
default:
$endian = "Your system 'endian' is unknown."
. "It may be some perverse Middle-Endian architecture.";
}
?>
-nathan