PHP can use pack and unpack functions to read and write binary files.
Today I want to deal with the problem of a binary file, so I need to use it and specifically understand the usage of pack. The usage of unpack is similar to this.
Simply put, the pack function can return binary data by giving a target format and corresponding parameters.
Here is an example to illustrate for four integers:
pack("L4", 0,1,2,3) pack("LLLL", 0,1,2,3) pack("L", 0).pack("L", 1).pack("L", 2).pack("L", 3)
The above processing results are the same, that is, format is the format that describes the following data.
As for what format can be used, you will know when you look at format characters.
For example, a 30-character pack("a30", "https://") means this, it's very simple
The official statement of the pack function is as follows:
Quote pack (PHP 3, PHP 4, PHP 5) pack -- Pack data into binary string Description string pack ( string format [, mixed args [, mixed ...]] ) Pack given arguments into binary string according to format. Returns binary string containing data. The idea to this function was taken from Perl and all formatting codes work the same as there, however, there are some formatting codes that are missing such as Perl's "u" format code. The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. For a, A, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string. Currently implemented are sheet 1. pack() format characters Code Description 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) f float (machine dependent size and representation) d double (machine dependent size and representation) x NUL byte X Back up one byte @ NUL-fill to absolute position
If you are tired of reading English, let’s take a look at the corresponding Chinese explanation:
Quote pack()The function is:Compress data into a binary string。 a - NUL-padded string a - NUL- String full[padded string] A - SPACE-padded string A - SPACE- String full[padded string] h - Hex string, low nibble first h – Hexadecimal string,Low“Four digits”[low nibble first] H - Hex string, high nibble first H - Hexadecimal string,high“Four digits”[high nibble first] c - signed char c – Characters with symbols C - unsigned char C – 不Characters with symbols s - signed short (always 16 bit, machine byte order) s – Short pattern with symbols[short](Usually16Bit,By machine byte order) S - unsigned short (always 16 bit, machine byte order) S – 不Short pattern with symbols[short](Usually16Bit,Sort by machine bytes) n - unsigned short (always 16 bit, big endian byte order) n -不Short pattern with symbols[short](Usually16Bit,Press the bigendianByte sorting) v - unsigned short (always 16 bit, little endian byte order) v -不Short pattern with symbols[short](Usually16Bit,Press SmallendianByte sorting) i - signed integer (machine dependent size and byte order) i – Integer with signs(Determined by size and byte order) I - unsigned integer (machine dependent size and byte order) I – 不Integer with signs(Determined by size and byte order) l - signed long (always 32 bit, machine byte order) l– Long pattern with symbols[long](Usually32Bit,By machine byte order) L - unsigned long (always 32 bit, machine byte order) L – 不Long pattern with symbols[long](Usually32Bit,By machine byte order) N - unsigned long (always 32 bit, big endian byte order) N – 不Long pattern with symbols[long](Usually32Bit,Press the bigedianByte order) V - unsigned long (always 32 bit, little endian byte order) V– 不Long pattern with symbols[long](Usually32Bit,Press SmalledianByte order) f - float (machine dependent size and representation) f –floating point(Determined by size and byte order) d - double (machine dependent size and representation) d – Double precision(Determined by size and byte order) x - NUL byte x – Empty bytes[NUL byte] X - Back up one byte X- The next byte[Back up one byte] @ - NUL-fill to absolute position @ - NUL- 添加到一个绝对Bit置[absolute position]
The sample code is as follows:
<?php $code=array( "username"=>array("A7","Zhang San adfb12"), "pass"=>array("A10","asdf*#1"), "age"=>array("C","23"), "birthday"=>array("I","19900101"), "email"=>array("A50","")); $stream=join("\0",parkByArr($code)); echo $stream,strlen($stream); file_put_contents("",$stream);//Save the stream for easier reading belowfunction parkByArr($arr) { $atArr=array(); foreach ($arr as $k=>$v) { $atArr[]=pack($v[0],$v[1]); } return $atArr; } function getAscill($str) { $arr=str_split($str); foreach ($arr as $v) { echo $v,"=",ord($v),"\n"; } } $code=array( "username"=>array("A20"), "pass"=>array("A10"), "age"=>array("C"), "birthday"=>array("I"), "email"=>array("A50")); $stream=file_get_contents(""); var_dump(parkByArr($stream,$code)); function parkByArr($str,$code) { $Arr=explode("\0",$str); $atArr=array(); $i=0; foreach ($code as $k=>$v) { $atArr[$k]=unpack($v[0],$Arr[$i]); $i++; } return $atArr; }