Current Location: Home> Latest Articles> Examples of custom structure packaging and parsing: pack() + unpack()

Examples of custom structure packaging and parsing: pack() + unpack()

M66 2025-05-31

In PHP, the pack() function is a very powerful tool for packaging data into binary strings in a specified format. It is often used for network transmission, file writing, or interaction with binary protocols. Correspondingly, the unpack() function can parse the packaged binary string back to structured data. This article will introduce in-depth how to use pack() and unpack() to achieve packaging and parsing of custom structures, and help you better understand its application through specific examples.


Introduction to pack() function

The pack() function packages one or more data into binary strings based on the format string. Common formats include:

  • a : NUL filled string (string fixed length)

  • A : Space filled string (string fixed length)

  • C : Unsigned characters (1 byte)

  • c : Signed characters (1 byte)

  • S : Unsigned short integer (2 bytes)

  • s : signed short integer (2 bytes)

  • L : Unsigned long (4 bytes)

  • l : signed long integer (4 bytes)

  • N : Unsigned long integer (4 bytes, network endianness)

  • n : Unsigned short integer (2 bytes, network endianness)


Custom structure example

Suppose we have a simple structure definition as follows:

  • User ID (unsigned long integer, 4 bytes)

  • Status (unsigned characters, 1 byte)

  • Balance (signed short integer, 2 bytes)

  • Name (fixed length 10 byte string)

How to package these data with pack() and parse it with unpack() ?


Code Example

 <?php
// Define data
$userId = 123456789;
$status = 1;
$balance = -250;
$name = "Zhang San";

// usepack()Packaging data
// Format description:
// N - userID,Unsigned long integers of network endianness(4byte)
// C - state,Unsigned characters(1byte)
// s - Balance,Signed short integer(2byte)
// a10 - Name,Fixed length10byte,NULfilling
$packedData = pack('NCsa10', $userId, $status, $balance, $name);

// Print the hexadecimal representation of the packaged binary data,Easy to observe
echo "Packed data(hexadecimal): " . bin2hex($packedData) . PHP_EOL;

// Simulate data transmission over the network,Unpack after receiving
$unpackedData = unpack('NuserId/Cstatus/sbalance/a10name', $packedData);

// Print unpacked array
print_r($unpackedData);
?>

Output example

 Packed data(hexadecimal): 075bcd1501ff06e58f5a696e67
Array
(
    [userId] => 123456789
    [status] => 1
    [balance] => -250
    [name] => Zhang San
)

Detailed description

  • The N format represents an unsigned long integer (4 bytes) and adopts network byte order (big endian), which is often used for cross-platform data transmission to ensure the consistent byte order.

  • C means unsigned characters, 1 byte, for status fields.

  • s represents a signed short integer (2 bytes), where the balance is negative, and signed values ​​are supported.

  • a10 represents a fixed-length string. When the string is insufficient, it will be filled with \0 .

The unpack() function unpacks data through the same format string, returns an associative array, and the key names can be customized to facilitate access to each field.


Tips and precautions

  1. Byte order issue <br> If the data needs to be transmitted between different machine architectures, it is recommended to use the network endian ( N , n ) format to ensure compatibility between the small and small ends.

  2. String padding <br> Using the a format will fill in the insufficient part of the string with \0 , while A will fill in the space. Choose the appropriate filling method to avoid parsing errors.

  3. Fixed data length <br> The packaging format must be strictly consistent with the unpacking format, especially the string length must be the same, otherwise data parsing will cause errors.

  4. Debugging skills <br> You can use bin2hex() to convert binary data into hexadecimal to facilitate debugging and verification.


Combined with URL scenario example

If you need to package a data packet, which contains a URL field, and the domain name part needs to be replaced with m66.net , you can first process the string and then package it: