Re: pack it in

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Jan 24, 2008 7:13 PM, Jochem Maas <jochem@xxxxxxxxxxxxx> 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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux