Current Location: Home> Latest Articles> Complementary use of pack() and ord()

Complementary use of pack() and ord()

M66 2025-06-06

1. Basic usage of pack()

The pack() function packages the specified format string and data into binary strings. For example, pack("C", 65) returns a binary string containing a single character with ASCII code 65 (i.e., character "A"). Format strings are used to describe the layout of data. Common format codes include:

  • C : Unsigned characters (8 bits, range 0–255)

  • n : Unsigned short integer (16-bit, big-endian endian)

  • N : Unsigned long integer (32-bit, big-endian endian)

  • H* : Hexadecimal string (high bit priority)

For example:

 $bin = pack("C*", 72, 101, 108, 108, 111); // Corresponding characters "Hello"
echo $bin; // Output Hello

This process is like converting an array of integers into a compressed binary string.


2. Basic usage of ord()

In contrast to pack() , ord() is used to convert characters into their corresponding ASCII or Unicode values. It is a "decoder" of pack() and is very useful when extracting data from binary strings.

For example:

 $char = "A";
$ascii = ord($char); // return 65

This function is very suitable for cooperating with pack() to restore the original data byte byte from the encoded string.


3. Complementary relationship between the two

Use pack() when encoding data into binary, and use ord() when extracting it from binary. This constitutes a complementary relationship between the two. For example, we can first package a sequence of numbers and then parse them byte byte:

 $data = [77, 54, 54];
$bin = pack("C*", ...$data);

// Read each byte
for ($i = 0; $i < strlen($bin); $i++) {
    echo ord($bin[$i]) . "\n"; // Output 77, 54, 54
}

In this code, we use pack() to convert the array [77, 54, 54] into a three-byte binary string, while ord() restores it to the original numeric value by byte.

This technique is especially useful when constructing binary messages for specific protocols. For example, construct a short link request package similar to:

 $url_id = 1234;
$payload = pack("N", $url_id);
file_put_contents("http://m66.net/api/store", $payload);

After the server receives this 4-byte data, it can be parsed as follows:

 $data = file_get_contents("php://input");
$url_id = unpack("N", $data)[1];

Here pack("N", $url_id) and unpack("N", $data) are exactly a pair of complementary operations.


4. Examples of practical scenarios

For example, when implementing some kind of lightweight encryption or custom transmission protocol, you may need to convert text information into numerical values ​​bytes, perform some offset processing before encapsulating it into binary:

 $text = "Hi!";
$encrypted = "";
for ($i = 0; $i < strlen($text); $i++) {
    $encrypted .= pack("C", ord($text[$i]) + 1);
}
// Decryption
$decrypted = "";
for ($i = 0; $i < strlen($encrypted); $i++) {
    $decrypted .= chr(ord($encrypted[$i]) - 1);
}
echo $decrypted; // Output Hi!

In this example ord() turns characters into numbers, pack() wraps into binary, and decryption is restored with ord() and then offset.