pack() Function
string pack ( string $format [, mixed $args [, mixed $... ]] )
The pack() function converts arguments into a binary string according to the specified format.
Format Codes
| Code | Description |
|---|---|
| a | NUL-padded string |
| A | Space-padded string |
| h | Hex string (low nibble first) |
| H | Hex string (high nibble first) |
| c | Signed char |
| C | Unsigned char |
| s | Signed short (16-bit, machine byte order) |
| S | Unsigned short (16-bit, machine byte order) |
| n | Unsigned short (16-bit, big endian) |
| v | Unsigned short (16-bit, little endian) |
| i | Signed integer (machine dependent) |
| I | Unsigned integer (machine dependent) |
| l | Signed long (32-bit, machine byte order) |
| L | Unsigned long (32-bit, machine byte order) |
| N | Unsigned long (32-bit, big endian) |
| V | Unsigned long (32-bit, little endian) |
| q | Signed long long (64-bit, machine byte order) |
| Q | Unsigned long long (64-bit, machine byte order) |
| J | Unsigned long long (64-bit, big endian) |
| P | Unsigned long long (64-bit, little endian) |
| f | Float (machine dependent) |
| d | Double (machine dependent) |
| x | NUL byte |
| X | Backup one byte |
| Z | NUL-padded string (new in PHP 5.5) |
| @ | NUL-fill to absolute position |
Byte Order Explained
Byte order refers to how multi-byte data is stored in memory. Consider two characters 'A' (0100 0001) and 'B' (0100 0010). They can be stored as 0100 0001 0100 0010 or 0100 0010 0100 0001 - this sequence defines byte order.
- High/Low Bytes: In 'AB', 'A' is the high byte, 'B' the low byte
- Memory Addresses: High addresses store most significant bytes
- Big Endian: High byte at low memory address (network standard)
- Little Endian: Low byte at low memory address (common on x86)
- Host Byte Order: Native byte order of the current machine
String Formatting Examples
// NUL-padded string
$result = pack('a6', 'test');
var_dump($result); // string(6) "test\0\0"
// Space-padded string
$result = pack('A6', 'test');
var_dump($result); // string(6) "test "
Hexadecimal Packing
$binary = pack('H4', '4869'); // 'Hi' in hex
var_dump($binary); // string(2) "Hi"
Character Packing
$chars = pack('c3', 65, 66, 67);
var_dump($chars); // string(3) "ABC"
Integer Packing
$packed = pack('L', 305419896);
var_dump($packed); // 4-byte binary string
Floating Point Numbers
$float_data = pack('f', 123.456);
$unpacked = unpack('f', $float_data);
var_dump($unpacked[1]); // float(123.456001)
Special Format Codes
// NUL byte
$nul = pack('x');
var_dump(ord($nul)); // int(0)
// NUL-padded with length limit
$limited = pack('Z3', 'hello');
var_dump($limited); // string(3) "he"
// Position-based padding
$padded = pack('@8');
var_dump(strlen($padded)); // int(8)
unpack() Function
array unpack ( string $format , string $data )
The unpack() function reverses the packing process, converting binary data back to PHP values.
Basic Unpacking
$packed = pack('L3', 100, 200, 300);
$result = unpack('L3', $packed);
print_r($result);
/* Output:
Array
(
[1] => 100
[2] => 200
[3] => 300
)
*/
Named Unpacking
$binary = pack('L3', 10, 20, 30);
$data = unpack('Lfirst/Lsecond/Lthird', $binary);
print_r($data);
/* Output:
Array
(
[first] => 10
[second] => 20
[third] => 30
)
*/
Practical Applications
- Inter-language Communication: Binary data exchange with other programming languages
- Data Encryption: Custom binary formats provide basic obfuscation
- Storage Optimization: Binary representation uses less space than string formats
- Network Protocols: Implementing custom binary protocols
- File Format Handling: Reading and writing binary file formats