In PHP development, there are times when we need to convert integers into binary string format for data packing, network transmission, or file writing. PHP’s built-in pack() function is the perfect tool for this task. This article introduces the basic usage of the pack() function, focusing on how to convert integers to binary strings, and provides practical examples to illustrate its applications.
The pack() function is used to pack data into a binary string. Its syntax is as follows:
string pack(string $format, mixed ...$values)
$format: A format string that specifies how the subsequent arguments should be packed.
$values: The data to be packed.
pack() supports various format characters. This article mainly focuses on several key characters used for packing integers.
When dealing with integers, here are some commonly used format characters:
C: Unsigned char (1 byte, range 0–255)
n: Unsigned short (2 bytes, network byte order)
v: Unsigned short (2 bytes, host byte order)
N: Unsigned long (4 bytes, network byte order, big-endian)
V: Unsigned long (4 bytes, host byte order, little-endian)
J: Unsigned 64-bit integer (supported from PHP 5.6+)
For example, to convert the integer 1234 into a 2-byte network byte order binary string:
$binary = pack("n", 1234);
Suppose we want to convert 4294967295 (the maximum value of a 32-bit unsigned integer) into network byte order:
$int = 4294967295;
$binary = pack("N", $int);
echo bin2hex($binary); // Output: ffffffff
Assume we want to create a binary data file containing a user ID (4 bytes), level (1 byte), and status (1 byte):
$userId = 1001;
$level = 5;
$status = 1;
<p>$data = pack("NCC", $userId, $level, $status);<br>
file_put_contents("user_data.bin", $data);<br>
The generated file will contain exactly 7 bytes of binary data, making it ideal for embedded systems or cross-platform data exchange.
For instance, if you are developing a game server communication protocol, the client needs to send a data packet with the following structure:
Header (2 bytes, fixed value 0xABCD)
Data length (2 bytes)
User ID (4 bytes)
Token string (variable length)
You can construct it as follows:
$header = 0xABCD;
$userId = 12345;
$token = "abc123token";
$length = 4 + strlen($token);
<p>$packet = pack("nnN", $header, $length, $userId) . $token;</p>
<p>// Send to server address, e.g., <a rel="noopener" target="_new" class="" href="http://m66.net/api/receive_packet">http://m66.net/api/receive_packet</a><br>
The server can use unpack() to decode and restore the data upon receipt.
Endian Issues: When dealing with cross-platform or network communication, be sure to use N (big-endian) or V (little-endian) format to ensure consistency.
Type Range Limits: Ensure that the integer values fall within the allowed range for the specified format character to avoid overflow or truncation.
Using with unpack(): The pack() and unpack() functions are designed to be used together for convenient encoding and decoding.
With the pack() function, PHP developers can easily convert integers into binary strings for efficient data packing. This is especially important when handling low-level protocols, generating file formats, or communicating with non-PHP systems. Mastering the use of format characters is a crucial step toward proficient PHP low-level data processing.