September 26, 2024
Quick Reference
The PHP unpack() function unpacks data from a binary string.
<?php
$data = 'PHP';
print_r(unpack('C*', $data));
?>
Syntax
unpack(format, data)
Parameters
Parameter Description
format Specifies the format to use when unpacking data:
a - NUL-padded string
A - SPACE-padded string
h - Hex string, low nibble first
H - Hex string, high nibble first
c - signed char
C - unsigned char
s - signed short (always 16 bit, machine byte order)
S - unsigned short (always 16 bit, machine byte order)
n - unsigned short (always 16 bit, big endian byte order)
v - unsigned short (always 16 bit, little endian byte order)
i - signed integer (machine dependent size and byte order)
I - unsigned integer (machine dependent size and byte order)
l - signed long (always 32 bit, machine byte order)
L - unsigned long (always 32 bit, machine byte order)
N - unsigned long (always 32 bit, big endian byte order)
V - unsigned long (always 32 bit, little endian byte order)
q - signed long long (always 64 bit, machine byte order)
Q - unsigned long long (always 64 bit, machine byte order)
J - unsigned long long (always 64 bit, big endian byte order)
P - unsigned long long (always 64 bit, little endian byte order)
f - float (machine dependent size and representation)
g - float (machine dependent size, little endian byte order)
G - float (machine dependent size, big endian byte order)
d - double (machine dependent size and representation)
e - double (machine dependent size, little endian byte order)
E - double (machine dependent size, big endian byte order)
x - NUL byte
X - Back up one byte
Z - NUL-padded string
@ - NUL-fill to absolute position
data Specifies the binary data to be unpacked (required)
offset Specifies where to start unpacking from (default is 0)
PHP Notes:
When using PHP, single or double quotation marks are acceptable and work identically to one another; choose whichever you prefer, and stay consistent
Arrays count starting from zero NOT one; so item 1 is position [0], item 2 is position [1], and item 3 is position [2] … and so on
We’d like to acknowledge that we learned a great deal of our coding from W3Schools and TutorialsPoint , borrowing heavily from their teaching process and excellent code examples. We highly recommend both sites to deepen your experience, and further your coding journey. We’re just hitting the basics here at 1SMARTchicken.