How does PHP handle binary data? Today I will bring you an implementation method for processing binary data in PHP. Share it with everyone and give it as a reference. I hope to be helpful.
PHP needs to use pack() and unpack() to process binary data.
pack() is used to convert data into binary data. The usage method is as follows:
pack("LL", 0 ,1);
pack(“C”, a);
unpack() can parse binary data into a relational array. It accepts 2 parameters and is used as follows:
$arr = unpack(“Chead”, $binstream); //Read the first byte
$arr = unpack(“Chead/C3string/C4number”, $binstream); // Read 8 bytes, which can be separated by slashes
The first parameter table of the pack() and unpack() functions is as follows
■a: NULL-filled byte string
■A: Space-filled byte string
■h: Hexadecimal number, low nibble first
■ H: Hexadecimal number, high nibble first
■c: Signed character
■C: Unsigned character
■s: Signed short (always 16 bits, machine byte order)
■S: Unsigned short (always 16 bits, machine byte order)
#■n: Unsigned short (always 16 bits, big-endian)
■v: Unsigned short (always 16 bit, little endian)
■I: signed integer (machine dependent size and endianness)
■I: unsigned integer (machine dependent Relevant size and endianness)
■l: signed long (always 32 bits, machine endianness)
■L: unsigned long Integer type (always 32 bits, machine byte order)
■N: Unsigned long integer type (always 32 bits, big endian byte order)
■V: Unsigned long (always 32 bits, little endian)
■f: Floating point (machine dependent size and representation)
■d: Double (machine dependent size and representation)
■x: Null byte
■X: Go back one byte
■@: Fill absolute positions with NULL
Related recommendations:
Some neglected PHP functions (organized)
php Detailed explanation of file reading series methods
php File splitting and merging (resumable upload at breakpoints )
The above is the detailed content of How PHP handles binary data. For more information, please follow other related articles on the PHP Chinese website!