packDescriptionpack() transforms a list of values into a sequence of bits based on a user-defined format. It's a powerful tool that allows developers to work directly with binary data. pack() can be used for many purposes - from simple tasks such as trimming and padding strings to more complex tasks such as altering binary files. Calls to the function look like this: $binary_string = pack ('C4', 128, 9, 176, 32);The first argument contains the format string. The format string defines how the remaining arguments should be converted into binary data. In the previous example, the format string specifies that the remaining four arguments should be converted to a sequence of four unsigned bytes. The format string consists of one or more format codes. The format codes can be grouped into three categories. The first group is used to create numeric values of a specific byte length, order, and precision. The second group is used to create strings of character or hex data. The final group is used to manipulate the position of the data in the binary string. (See the following tables for a full description of the various format codes.) Position type format codes
Numeric type format codes
String type format codes
pack() is based on the Perl function of the same name and behaves in a similar fashion.There are a few notable differences. Perl allows spaces in the format string; PHP doesn't. Additionally, Perl supports some format codes that PHP doesn't. The codes are listed in the following table, along with a description to help developers who are converting Perl scripts to PHP.
ExampleExample 900. Create a string from binary data echo pack ( "C*", bindec ('01010000'), bindec ('01001000'), bindec ('01010000'), bindec ('00100000'), bindec ('00110100') ); Output: PHP 4 Example 901. Determine whether a machine is big-endian, little-endian, or middle-endian <?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 ('V', $abyz): echo 'Your system is Big-Endian.'; break; default: $endian = "Your system 'endian' is unknown." . "It may be some perverse Middle-Endian architecture."; } ?>
PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing
(Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch).
This material may be distributed only subject to the terms and conditions set forth
in the Open Publication License, v1.0 or later (the latest version is presently available at
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4
for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All
persons and entities shall have neither liability nor responsibility to any person or entity with respect to
any loss or damage arising from the information contained in this book.
|