Current Location: Home> Latest Articles> How to Use the pack() Function in PHP to Convert a String to Hexadecimal Format?

How to Use the pack() Function in PHP to Convert a String to Hexadecimal Format?

M66 2025-06-15

In PHP programming, there are times when we need to convert a string to hexadecimal format for low-level data processing, network transmission, or interaction with hardware devices. PHP provides the powerful pack() function, which helps us easily achieve this.

This article will introduce how to use the pack() function to convert a string to hexadecimal format and demonstrate its usage with examples.

1. What is the pack() Function?

The pack() function is used to pack data into a binary string. It converts the parameters into binary format based on the specified format and returns the result. It is commonly used in scenarios like generating binary protocol packets or writing file data.

Function prototype:

string pack(string $format, mixed $args, mixed ...$args)
  • $format is a format string that specifies how to pack the data.

  • $args are the data to be packed, which can be multiple values.

2. The Principle of String to Hexadecimal Conversion

Although pack() is mainly used for packing data, when combined with unpack() or bin2hex(), it can easily achieve the conversion of a string to hexadecimal format.

The simple idea is:

  • Directly use bin2hex() to convert the string to hexadecimal;

  • Or use pack() to pack the string with a specific format, then use bin2hex() to output the result.

3. Convert String to Hexadecimal Using pack()

The format character H* in pack() indicates hexadecimal string encoding, i.e., converting a hexadecimal string into binary data.

If you already have a string to convert to hexadecimal, you can first use pack() in combination with unpack(), which can also achieve a similar effect.

Here’s an example showing how to convert a string to hexadecimal using pack() and bin2hex():

<?php
// Original string
$str = "hello world";
<p>// Use pack() to pack the string, format 'a*' means a NUL-padded string<br>
$packed = pack('a*', $str);</p>
<p>// Use bin2hex() to convert to hexadecimal representation<br>
$hex = bin2hex($packed);</p>
<p>echo "Original string: " . $str . "\n";<br>
echo "Hexadecimal representation: " . $hex . "\n";<br>
?><br>

Output result:

Original string: hello world
Hexadecimal representation: 68656c6c6f20776f726c64

4. Commonly Used pack() Format Characters

  • a: NUL-padded string

  • A: Space-padded string

  • H: Hexadecimal string, low-endian

  • h: Hexadecimal string, high-endian

  • C: Unsigned character (8-bit)

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

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

If you want to directly process hexadecimal strings to binary using pack(), you can use something like:

<?php
$hexStr = "68656c6c6f";
$bin = pack('H*', $hexStr);
echo $bin; // Outputs the corresponding string hello
?>

5. Practical Use Cases

If you need to send hexadecimal data to an interface or store a string in hexadecimal form, the above methods are quite effective.

For example, when getting data from an API:

<?php
$url = "https://m66.net/api/data";
$response = file_get_contents($url);
<p>// Convert the response content to hexadecimal<br>
$hexResponse = bin2hex($response);</p>
<p>echo "Hexadecimal format of the response content:\n";<br>
echo $hexResponse;<br>
?><br>

6. Conclusion

  • The pack() function can be used to pack strings or data into binary according to a specific format.

  • To convert a string to hexadecimal, the common method is to first pack() (with format 'a*') and then use bin2hex() to convert it.

  • You can also use pack('H*', $hexString) to convert a hexadecimal string back to binary.

  • Based on the actual needs, choose the format character appropriately and flexibly use pack() and bin2hex().

Through this article, we hope you have gained a solid understanding of the pack() function in PHP and the techniques for converting strings and hexadecimal values.