In PHP, pack() and unpack() functions are often used to process binary data, and are especially suitable for use in scenarios such as network communication, file reading and writing, data encoding, etc. where precise control of bytes is required. This article will explain in detail the usage of these two functions and attach a complete format code comparison table to help you process binary data more efficiently.
pack() : Package data into binary string.
unpack() : Unpack the binary string into a PHP array.
Both use format codes to define the structure of the data, which determine how the data is encoded/decoded.
$data = pack("C*", 72, 101, 108, 108, 111); // Will ASCII Convert code to string "Hello"
echo $data; // Output: Hello
grammar:
string pack(string $format, mixed $values...)
$data = "Hello";
$result = unpack("C*", $data);
print_r($result);
Output:
Array
(
[1] => 72
[2] => 101
[3] => 108
[4] => 108
[5] => 111
)
grammar:
array unpack(string $format, string $data, int $offset = 0)
Format code | illustrate | Byte length | Large and small end |
---|---|---|---|
a | NUL Fill the string (not removing NUL) | Specify length | none |
A | Spaces fill strings (the tail spaces will be removed) | Specify length | none |
h | Hexadecimal string, each character represents 4 bits | Every two digits = 1 byte | none |
H | Caps hexadecimal, each character indicates 4 bits | Every two digits = 1 byte | none |
c | Signed characters (8 bits) | 1 byte | none |
C | Unsigned characters (8 bits) | 1 byte | none |
s | Signed short integer (16 bits) | 2 bytes | Host byte order |
S | Unsigned short integer (16-bit) | 2 bytes | Host byte order |
n | Unsigned short integer (16-bit), network byte order | 2 bytes | Big endian |
v | Unsigned short integer (16-bit), little-endian byte order | 2 bytes | Little endian |
i | Signed integers (machine-related) | 4 bytes or 8 bytes | Host byte order |
I | Unsigned integers (machine-related) | 4 bytes or 8 bytes | Host byte order |
l | Signed long integer (32 bits) | 4 bytes | Host byte order |
L | Unsigned long (32-bit) | 4 bytes | Host byte order |
N | Unsigned long integer (32 bits), network byte order | 4 bytes | Big endian |
V | Unsigned long integer (32 bits), little endian byte order | 4 bytes | Little endian |
f | Single-precision floating point number (32 bits) | 4 bytes | Host byte order |
d | Double precision floating point number (64 bits) | 8 bytes | Host byte order |
x | Fill in bytes (skip 1 byte) | 1 byte | none |
X | Rewind 1 byte | 1 byte | none |
@ | Set the absolute position (skip/fall back to a position) | variable | none |
$ip = "192.168.1.1";
$packed = pack("C4", ...explode('.', $ip));
echo bin2hex($packed); // Output c0a80101
$unpacked = unpack("C4", $packed);
echo implode('.', $unpacked); // Output 192.168.1.1
$binary = file_get_contents("https://m66.net/example/file.bin");
$header = unpack("a4signature/Nsize", $binary);
print_r($header);
The format code can be modified with quantity, for example:
C4 : represents 4 unsigned characters
A10 : padded string with spaces representing 10 characters
@8 : Jump to the 8th byte position
You can also use * to read as much as possible:
$data = unpack("C*", "Hello"); // Read all bytes
PHP's pack() and unpack() are powerful binary processing tools. Being familiar with its format code and usage can help you process underlying data more flexibly. Whether it is parsing protocols, storing structured data, or building efficient network communication protocols, these two functions are very practical tools.
Although there are many format codes, mastering the commonly used types (such as C , n , N , f , d ) can meet most needs. It is recommended to use bin2hex() and hex2bin() together in development, for debugging is more convenient.