In PHP, the pack() function is used to package data into binary strings in the specified format. This is useful when dealing with network transfers, file writing, and interactions with C or other low-level languages. This article will explain in detail the usage of the pack() function and combine examples to illustrate how to package data into binary format.
The basic syntax of pack() is as follows:
string pack(string $format, mixed $values, mixed ...$values)
$format : Specifies the format of the data, consisting of one or more format codes.
$values : The value to be packaged can be multiple parameters.
The return value is a binary string packaged in the specified format.
Format code | describe | Example |
---|---|---|
a | NUL character filled string | pack('a4', 'abc') |
A | Space filled string | pack('A4', 'abc') |
c | Signed characters (1 byte) | pack('c', 65) |
C | Unsigned characters (1 byte) | pack('C', 65) |
s | Signed short integer (2 bytes) | pack('s', 1234) |
S | Unsigned short integer (2 bytes) | pack('S', 1234) |
i | Signed integer (machine word length) | pack('i', 123456) |
I | Unsigned integer (machine word length) | pack('I', 123456) |
l | Signed long integer (4 bytes) | pack('l', 123456) |
L | Unsigned long integer (4 bytes) | pack('L', 123456) |
f | Floating point number (4 bytes) | pack('f', 3.14) |
d | Double precision floating point number (8 bytes) | pack('d', 3.14) |
n | Unsigned short integer (16-bit big-endian endian) | pack('n', 1234) |
N | Unsigned long integer (32-bit big-endian endian) | pack('N', 1234) |
<?php
// Pack one length4string and one32bit unsigned big endian integer
$packed = pack('a4N', 'test', 123456789);
echo bin2hex($packed);
?>
Here 'a4' means packing a string of length 4, the insufficient part is filled with NUL characters; 'N' means packing a 32-bit unsigned big-endian integer. bin2hex() converts binary content into hexadecimal for easy viewing.
Suppose you need to construct a simple network packet, including the command type (1 byte) and the data length (4 bytes big endian), and then the data body:
<?php
$command = 1; // Command Type,1byte
$data = "Hello, m66.net!"; // Data body
$length = strlen($data); // Data length
// 打包Command Type和Data length
$header = pack('CN', $command, $length);
// Splicing complete data packets
$packet = $header . $data;
// Hexadecimal representation of output package
echo bin2hex($packet);
?>
In the above code, 'C' package command type (1 byte unsigned integer), and 'N' package data length (4 byte big-endian integer). Data bodies are spliced as is.
<?php
$number = 65535;
$packed = pack('n', $number); // 按大端byte序打包为16Unsigned integer
echo 'Packed data: ' . bin2hex($packed) . PHP_EOL;
$unpacked = unpack('nvalue', $packed); // Unpacking
echo 'Unpacked value: ' . $unpacked['value'] . PHP_EOL;
?>
pack() is a powerful tool for manipulating binary data, especially suitable for network programming and underlying data processing. By skilled in using format code, you can accurately control the byte arrangement and type of data.