PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




pack

Name

pack — Creates a binary string from a list of values.

Synopsis

mixed pack(format, args[, args]);
string format: Format to use to pack the data
mixed args: Piece of data to be packed into the string
args (optional): Zero or more additional arguments to be packed into the string

Returns

Binary string; FALSE on error

Description

pack() 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

Format Code Description
@ Move to an absolute position within the string. The position should be indicated by an integer immediately following the format code. When using this format code, remember that the first index in the binary string is 0.
$binary_string = pack ('AA@0A', 'a', 'b', 'c');
# Hex output is: 63# Character output is: c

$binary_string = pack ('AA@1A', 'a', 'b', 'c');
# Hex output is: 61 63# Character output is: ac

$binary_string = pack ('@9CCC', 0x61, 0x62, 0x63);
# Hex output is: 00 00 00 00 00 00 00 00 00 61 62 63# Character output is: abc
x Insert a NULL byte.
$binary_string = pack ('AxAxA', 'f', 'o', 'o');
# Hex output is: 66 00 6f 00 6f# Character output is: foo
X Move one byte backward in the string.
$binary_string = pack ('AAXA', 'a', 'b', 'c');
# Hex output is: 61 63# Character output is: ac

$binary_string = pack ('C2X2A', 'a', 'b', 'c');
# Hex output is: 63# Character output is: c

$binary_string = pack ('VXXXC*', 0x41, 0x42, 0x43);
# Hex output is: 41 42 43# Character output is: ABC


Numeric type format codes

Format Code Length Byte Order Signed Description
c 8 bits not applicable yes Signed character/byte.
C 8 bits not applicable no Unsigned character/byte.
d machine machine yes Native double. Length and byte order vary based on the machine architecture.
f machine machine yes Native float. Length and byte order vary based on the machine architecture.
i machine machine yes Native signed integer. Length and byte order vary based on the machine architecture.
I (uppercase i) machine machine no Native unsigned integer. Length and byte order vary based on the machine architecture.
l (lowercase L) 32 bits machine yes 32-bit signed native long. Byte order varies based on the machine architecture.
L 32 bits machine no 32-bit unsigned native long. Byte order varies based on the machine architecture.
n 16 bits big endian yes 16-bit unsigned short in 'Network' byte order (big endian).
N 32 bits big endian no 32-bit unsigned long in 'Network' byte order (big endian).
s 16 bits machine yes 16-bit signed native short. Byte order varies based on the machine architecture.
S 16 bits machine no 16-bit unsigned native short. Byte order varies based on the machine architecture.
v 16 bits little endian yes 16-bit unsigned short in 'Vax' byte order (little endian).
V 32 bits little endian no 32-bit unsigned long in 'Vax' byte order (little endian).


String type format codes

Format Code Description
a A NULL-padded string.
$binary_string = pack ('a10', 'Hi Mom!');
# Hex output is: 48 69 20 4d 6f 6d 21 00 00 00# Character output is: Hi Mom!
A A space-padded string.
$binary_string = pack ('A10', 'Hi Mom!');
# Hex output is: 48 69 20 4d 6f 6d 21 20 20 20# Character output is: Hi Mom!
h A string of hex values - low nibble first. (That is, decimal 240 would be converted to hex 0F.)
H A string of hex values - high nibble first. (That is, decimal 240 would be converted to hex F0.)


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.

Format Code Description
b Bit string, low-to-high order (See PHP's decbin() function. This is also roughly similar to the Perl vec() function, which has no cognate in PHP. See the entry on unpack() for our version of vec() .)
B Bit string, high-to-low order (See PHP's decbin() function. This is also roughly similar to the Perl vec() function, which has no cognate in PHP. See the entry on unpack() for our version of vec() .)
p Pointer to a string. PHP currently supports nothing even remotely related to this.
P Pointer to a structure (fixed-length string). PHP currently supports nothing even remotely related to this.
u uuencoded string. PHP has no built-in UUEncode function.
w BER-format compressed integer. (For more information, do a web search for ISO 8825 Basic Encoding Rules.)


Version

PHP Version: 3+, 4+

See also

To unpack a packed string

unpack()

Example

Example 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.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.