Php uses pack to process binary files

Source: Internet
Author: User
This article describes how to use pack to process binary files in php. For more information, see

This article describes how to use pack to process binary files in php. For more information, see

The pack and unpack functions can be used to read and write binary files in php.
To solve the problem of a binary file today, we need to take a look at the usage of pack. The usage of unpack is similar to this.

Simply put, the pack function provides a target format and corresponding parameters to return binary data.

The following is an example of four integers:

Pack ("L4",) pack ("LLLL",) pack ("L", 0 ). pack ("L", 1 ). pack ("L", 2 ). pack ("L", 3)

The preceding processing result is the same. That is to say, format is the format of the subsequent data.
For details about the specific format, see format characters.
For example, a 30 character pack ("a30", "http://www.jb51.net"), is that meaning, 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 [, 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 table 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 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

I am tired of reading English. Let's take a look at the corresponding Chinese explanation below:

The function of referencing pack () is used to compress data into a binary string. A-NUL-padded string a-NUL-string fill [padded string] A-SPACE-padded string A-SPACE-string fill [padded string] h-Hex string, low nibble first h-hexadecimal string, low "four bits" [low nibble first] H-Hex string, high nibble first H-hexadecimal string, high "four-digit" [high nibble first] c-signed char c-character C-unsigned char C-character s-signed short (always 16 bit, machine byte order) s-Signed short mode [short] (usually 16 bits, In the machine byte order) S-unsigned short (always 16 bits, machine byte order) s-unsigned short mode [short] (usually 16 bits, sorted by machine bytes) n-unsigned short (always 16 bit, big endian byte order) n-unsigned short mode [short] (usually 16 bits, sorted by large endian bytes) v-unsigned short (always 16 bits, little endian byte order) v-Unsigned short mode [short] (usually 16 bits, sorted by small endian bytes) I-signed integer (machine dependent size and byte order) i-signed integer (determined by the size and byte order) I-unsigned integer (machine dependent size and byte order) i-unsigned integer (determined by the size and byte order) l-signed long (always 32 bit, machine byte order) l-signed long mode [long] (usually 32-bit, in the machine byte order) L-unsigned long (always 32 bit, machine byte order) l-long mode without symbols [long] (usually 32-bit, In the byte order of the machine) N-unsigned long (always 32 bit, big endian byte order) n-long mode without symbols [long] (usually 32 bits, in the order of large edian bytes) V-unsigned long (always 32 bits, little endian byte order) v-long mode without symbols [long] (usually 32-bit, in the small edian byte order) f-float (machine dependent size and representation) f-floating point (determined by size and byte sequence) d-double (machine dependent size and representation) d-double Precision (determined by size and byte sequence) x-NUL byte x-Null byte [NUL byte] X-Back up one byte X-Next byte [Back up one byte] @-NUL-fill to absolute position @-NUL -Add 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", "23"), "birthday" => array ("I", "19900101 "), "email" => array ("A50", "www.jb51.net"); $ stream = join ("\ 0", parkByArr ($ code); echo $ stream, strlen ($ stream); file_put_contents ("1.txt", $ stream); // Save the stream to facilitate the following reading function 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 ("\ 0", $ str); $ atArr = array (); $ I = 0; foreach ($ code as $ k => $ v) {$ atArr [$ k] = unpack ($ v [0], $ Arr [$ I]); $ I ++;} return $ atArr ;}

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.