In PHP, the pack() function is a powerful tool that can package data into binary strings in the specified format, and is widely used in scenarios such as data transmission, file writing, and network protocol implementation. This article focuses on how to use pack("C*") to package arrays and analyze their usage, practical techniques and common applications.
The basic syntax of the pack() function is as follows:
string pack ( string $format , mixed ...$values )
Where $format specifies the packaging format of the data, while "C" means converting the parameter to unsigned characters (i.e. 1 byte, range 0~255). "C*" means to package multiple values in unsigned character format, which is suitable for encoding each element in the array as a single byte.
For example:
$data = [65, 66, 67];
$packed = pack("C*", ...$data);
echo $packed; // Output ABC
In this code, 65 corresponds to ASCII character A , 66 corresponds to B , and 67 corresponds to C , so the final output is the string "ABC" .
To convert an array of numbers into binary data through pack("C*") , the general steps are as follows:
Prepare an array containing numeric values, the values in the array should be between 0 and 255.
Pass the array as multiple parameters to pack("C*") using parameter unpacking ( ... ).
pack() converts each number into one byte and returns a binary string.
Sample code:
<?php
$array = [72, 101, 108, 108, 111]; // correspond "Hello"
$binary = pack("C*", ...$array);
echo $binary; // Output: Hello
?>
Note: Pack() returns a binary string, and it may not be output directly using echo . If you want to view it in a visual way, you can use the bin2hex() function:
echo bin2hex($binary); // 48656c6c6f
If the array is empty, calling pack("C*", ...$array) will report an error. It is recommended to judge first:
if (!empty($array)) {
$binary = pack("C*", ...$array);
} else {
$binary = '';
}
pack("C*") requires that the element is an integer of 0~255, otherwise unexpected results will occur. You can use array_map to limit the scope first:
$array = array_map(function($v) {
return $v & 0xFF; // Keep only the lowest8Bit
}, $array);
Packaged data is often used in custom network protocols, and can convert integer arrays into byte streams and then send them to remote servers.
Example:
$data = [1, 2, 3, 4];
$packet = pack("C*", ...$data);
$socket = stream_socket_client("tcp://m66.net:1234");
fwrite($socket, $packet);
fclose($socket);
Sometimes it is necessary to write binary format files, such as picture header information, audio and video files, etc.
$header = [0x89, 0x50, 0x4E, 0x47]; // PNGFile Signature
file_put_contents('file.png', pack("C*", ...$header));
When a custom protocol sends multiple fields, it is very common to convert numbers to byte streams.
pack("C*") can convert byte arrays into strings, while unpack("C*", $str) can implement inverse operations, making it easier to encode and decode.
pack("C*") is used to package an array of integers into binary strings in byte format.
The array element must be between 0 and 255, and the empty array should be checked first.
Suitable for various scenarios such as network transmission, file writing, and protocol development.
It uses unpack("C*") to achieve two-way conversion, making it more flexible to process byte data.
By using pack("C*") rationally, you can efficiently process binary data, making PHP more powerful in low-level data interaction.