How PHP uses the pack to process binary files _php tips

Source: Internet
Author: User
Tags compact mixed pack string format unpack

The pack and unpack functions can be used in PHP reading and writing binaries.
Today to deal with a binary file problem, so need to use, specifically to understand the use of the pack, unpack usage is similar to this.

In short, the pack function is to give a target format, and the corresponding parameters, you can return the binary data.

The following example illustrates, 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 result is the same, in other words, format is the form that describes the data that follows.
As for the specific format can use what, look at the format characters know.
Like a 30-character pack ("A30", "http://www.jb51.net"), that's what it means, it's simple.

The official statement of the pack function is as follows:

Reference 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 is taken from Perl and all formatting codes work the same as there, however, there are, some Matting codes that are missing such as Perl ' s "u" format code. The format string consists of the format codes followed by a optional repeater argument. The repeater argument can be either a 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 POS Ition 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 table 1. Pack () format characters Code Description a nul-padded string a space-padded string h Hex string, low nibble A/h Hex string, high nibble A-C signed Char c unsigned char s igned short (always bit, machine byte order) S unsigned short (always bit, machine byte order) n unsigned short (a Lways bit, big endian byte order) v unsigned short (always bit, little endian byte order) I signed integer (machin e dependent size and byte order I unsigned integers (machine dependent size and byte order) L signed long (always bit , machine byte order L unsigned long (always bit, machine byte) N unsigned long (always-bit, big endian byt 
E order) V unsigned long (always 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

Tired of watching English, let's take a look at the corresponding Chinese explanation:

The reference pack () function is to compress data into a binary string. a-nul-padded string a-nul-strings fill [padded string] a-space-padded string a-space-strings fill [padded string] H-hex St Ring, Lower Nibble first H-hexadecimal string, low "four-bit" [Nibble first] h-hex string, hi nibble first H-16 binary string, high "four" [n  Ibble-C-signed Char C-signed character c-unsigned Char C-unsigned character s-signed short (always bit, machine byte Order) s– A short mode with a symbol [shorter] (usually 16 bits, in machine byte sequence) s-unsigned short (always bit, machine byte) s– without symbols (shorter)  (usually 16-bit, sorted by machine byte) n-unsigned short (always bit, large endian byte order) n-no sign-less (usually 16-bit, sorted by big endian byte) v- Unsigned short (always bit, little endian byte order) v-no sign-less (usually 16-bit, sorted by small endian byte) i-signed integer (M Achine dependent size and byte order) I-Compact signed integer (determined by size and byte sequence) i-unsigned integer (Machine dependent size and byte Orde R) I-Compact an unsigned integer (determined by size and byte order) l-signed long (always bit, machine byte) l– with symbolic length [long] (usually 32-bit, by machine wordOrder of nodes) l-unsigned long (always bit, machine byte order) l– without a symbol in a longer pattern [long] (usually 32 bits, in machine byte sequence) n-unsigned long (alway  S bit, large endian byte order) Benzo vary thiazole long pattern without symbols [long] (usually 32-bit, in big Edian byte sequence) v-unsigned long (always bit, little endian byte order) v– long mode [long] (usually 32 bits, in small edian byte sequence) f-float (machine dependent size and representation) F – floating-point (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-null byte [NUL byte] X-ba ck up one byte x followed by a bytes [back up a byte] @-Nul-fill to absolute position @-nul-added to an absolute position [absolute position]

The

Sample code is as follows:

 <?php $code =array ("username" =>array ("A7", "John Adfb12"), "Pass" =>array ("A10", "Asdf* #1"), "Age" =>array ("C", "a"), "Birthday" =>array ("I", "19900101"), "email" =>array ("A50", "
Www.jb51.net "));
$stream ("=join", Parkbyarr ($code));
Echo $stream, strlen ($stream);
 File_put_contents ("1.txt", $stream);//Save the stream for reading function Parkbyarr ($arr) {$ATARR =array () below;
 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 ("1.txt");
Var_dump (Parkbyarr ($stream, $code));
 function Parkbyarr ($STR, $code) {$Arr =explode ("$str");
 $ATARR =array ();
 $i = 0;
 foreach ($code as $k => $v) {$ATARR [$k]=unpack ($v [0], $ARR [$i]);
 $i + +;
return $ATARR; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.