How PHP uses pack to process binary files _php Tutorial

Source: Internet
Author: User
Tags unpack
PHP read-write binary files can use the pack and unpack functions.
Today to deal with a problem with a binary file, so need to use, specifically to understand the use of the pack, unpack usage similar to this.

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

For an example, 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 results are the same, that is, format is the formatting that describes the data that follows.
As for the specific format can be used, see format characters to know.
Like a 30-character pack ("A30", "http://www.jb51.net"), that's what this means, very simple.

The official declaration of the pack function is as follows:

Reference Pack (PHP 3, PHP 4, PHP 5) Pack--pack data into binary stringdescriptionstring pack (string format [, Mixed args [, mix Ed ...] ) 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 is some for Matting codes that is 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 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 be taken, for @ it's the absolute POS Ition where to put the next data, for everything else the repeat count specifies how many data arguments is consumed and Packed into the resulting binary string. Currently implemented is Form 1. Pack () format Characterscode 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-on-bit, machine-byte order) n unsigned short (always-bit, big endian byte order) v unsigned short (always-on 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-on, machine-byte order) L Unsi Gned long (always A-bit, machine-byte order) N unsigned long (always, big endian byte order) V unsigned long (alwa ys + 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 reading English, the following to see the corresponding Chinese explanation:

The function of the reference pack () function is to compress the data into a binary string. a-nul-padded string a-nul-strings filled [padded string] a-space-padded string a-space-strings filled [padded string] H-hex string , Low nibble first H – hex string, lower "four-bit" [nibble-H-hex string, high nibble first H-16 binary string, height "four-bit" [Hi nibble F IRST] c-signed Char c – signed character c-unsigned Char c – unsigned character s-signed short (always-on, machine-byte order) s- Short pattern with symbols [shorter] (usually 16 bits, in machine byte order) s-unsigned short (always-on, ML-byte order) s-shorter (usually 16-bit, sorted by machine byte) ) n-unsigned short (always-on-bit, big endian byte order) n-shorter pattern without a symbol (usually 16 bits, sorted by large endian bytes) v-unsigned a Lways-bit, little endian byte order) v-Short pattern without symbols (usually 16 bits, sorted by small endian bytes) i-signed integer (Machine dependent siz E and byte order) I – signed integers (determined by size and byte order) i-unsigned integer (Machine dependent size and byte order) I – unsigned integers (by size and byte Order) l-signed long (always the first bit, machine byte order) L – with the symbol of the longer mode [long] (usually 32 bits, in the Order of machines byte) l-unsigned long (always + bit, machine byte order) L – long (usually 32-bit, per-byte order) with no symbol (always n-unsigned bit, big endian byte) n – Long mode [long] (usually 32 bits, in large Edian byte order) with no symbol v-unsigned long (always, little endian byte order) V– no sign of longer mode [long] (usually is 32 bits, in small Edian byte order) f-float (machine dependent size and representation) F – floating point (determined by size and byte order) d-double (machine dependent SI  Ze and representation) d – double precision (determined by size and byte order) X-nul byte x – null byte [NUL byte] x-back up one byte x back 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", "Zhang San Adfb12"), "Pass" =>array ("A10", "asdf* #1"), "Age" =>array ("C "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 easy reading of function below 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 ("1.txt"), Var_dump (Parkbyarr ($stream, $code)), function Parkbyarr ($str, $code) {$Arr =explode ("]=unpack", $str), $atArr =array (); $i =0; foreach ($code as $k + $v) {$ATARR [$k] ( $v [0], $ARR [$i]); $i + +; } return $ATARR;}

http://www.bkjia.com/PHPjc/824655.html www.bkjia.com true http://www.bkjia.com/PHPjc/824655.html techarticle PHP Read-write binary files can use the pack and unpack functions. Today to deal with a binary file problem, so need to use, specifically to understand the use of the pack, unpack usage with this ...

  • 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.