The pack method of PHP

Source: Internet
Author: User
Tags pack string format

Today I'm working on this pack method, but I don't know how to write it.

pack--compressed data into the string in place.
Syntax: string Pack (string format, mixed [args] ...);
Parameter one: The format parameter indicates how the data is read to the
Parameter two: The data that will be compressed.

Type of parameter one
A fills the string blank with a NULL character
A fills the string blank with space characters (spaces)
H 16 carry string, low in front
H 16 carry string, high in front
C has a number character
C no-number characters
S has a short integer (16 bits, depending on the bit order of the computer)
S-unsigned short integer (16 bits, by computer's bit order)
n Short-unsigned integers (16-bit, high in the latter order)
V unsigned short integers (16-bit, low in the back order)
I have a number of integers (according to the Order and range of the computer)
I-No integer (in the order and range of the computer)
l have long integers (32 bits, by computer bit order)
L unsigned long integers (32 bits, by computer bit order)
N short-unsigned integers (32-bit, high in the latter order)
V unsigned short integers (32-bit, low in the back order)
F single precise floating-point number (depending on the scope of the computer)
D-Times exact floating-point number (depending on the scope of the computer)
X vacancy
X Rewind A
@ Fill in NULL characters to absolute position

A summary of the above parameters from the network, do not know which translation, in short, I personally do not recommend looking. The following is the contents of the PHP manual (all in English, although I do not understand, but a word of the translation of a word to understand, than to see the above translation strong)

A--nul-padded string
A--space-padded string
H--Hex string, low nibble
H--Hex string, high nibble
C--Signed Char
C--unsigned char
S--Signed short (always bit, machine byte)
S--unsigned short (always bit, machine byte)
N--unsigned short (always bit, big endian byte)
V--unsigned short (always bit, little endian byte)
I--signed integer (Machine dependent size and byte order)
I--unsigned integer (machine dependent size and byte order)
L--Signed long (always bit, machine byte order)
L--unsigned long (always bit, machine byte order)
N--unsigned long (always bit, big endian byte)
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

====================h and h========================
-------------------------H--H------------------------------
First look at Chinese translation
H 16 carry string, high in front
H 16 carry string, low in front

Read English again
H--Hex string, low nibble first 16, lowest in half byte (less than half byte in translation above, this half byte is important, nibble is half byte meaning)
H--Hex string, high nibble first 16, highest in half byte


H is a 4-bit read, and a 16-bit is 4-bit, so H is 4 bits at a time, and H2 is a 8 bit (that is, a byte).
Echo Pack ("H", 0x4);
Echo Pack ("H2", 65);
Echo Pack ("H2", 0x41);
Echo Pack ("H2", "41");
Echo Pack ("H2h2", 0x41, 0x42);
Echo Pack ("H2H2", 0x41,0x42); `
Echo Pack ("H3", 124);
Echo Pack ("H3", 124);

Output is as follows
@
E
E
A
Ef
Vf




First line: Pack ("H", 0x4), read a hexadecimal (4-bit) in 16-way and write to the string. Because 0x4 is 4-bit,
0x4 is converted to decimal 4. And a byte is 8 bits, so it automatically adds a bit of byte 8 bit length, followed by 4 bits added 0000 (remember. The byte must be fully replenished before the H of the pack method is to be performed. So the decimal 40 (why to convert to decimal in the read, I do not know, may pack method developers are so written), decimal 40 in the H-hexadecimal way to read, is 0x40, converted to ASCII code is @.

The second line: Pack ("H2", 65); 65 is decimal (h is read decimal by default, read in 16), so 65 is 0x65 after H, and converting to ASCII code is E.

The Third line: Pack ("H2", 0x41); 0x41 is hexadecimal, H2 is read 8 bits at a time (h default is read read decimal, read in 16), 0x41 converted to decimal 65, 65 is 0x65 after H, converted to ASCII code is E.

Line four: Pack ("H2", "41"); " 41 "is a string, H2 represents reading a 8-bit, but (h default is read-read decimal, read in 16 way, so the string 41 is converted to decimal 41 by H), and decimal 41 is 0x41 by H, which translates to ASCII code is a.

Line five: Pack ("H2H2", 0x41, 0x42), 0x41 and 0x42 altogether is 16 bits, the H2H2 reads two 8 bits, translates to the decimal 65 and 66, 65 is after H is the 0x65,66 by H is the 0x66, translates to the ASCII code is respectively EF.

Line Six: Pack ("H2H2", 0x41,0x42), 0x41 and 0x42 are two eight bits, h 16 carry string, low in front. H and H are almost the same as just a matter of before and after sorting. H is high in front, H is low in front. 0x41 and 0x42 are converted to decimal 65 and 66. Read here followed by H is not the same, H is low in front, H is high in front. Here to make a comparison, as follows:

Read pre-hexadecimal 0x41 0x42
Read before decimal 65 66
H read-after hexadecimal 0x65 0x66
H read-after hexadecimal 0x56 0x66
H read the binary 01100101 01100110 high before the first with a H read object as the basis, 4-bit as the unit, to exchange the high-low
H read the binary 01010110 01100110 low before the first time with a H-read object as the basis, 4-bit units, the high and low interchange

H is converted to hexadecimal 56 and 66, respectively, to binary 01010110 and 01100110 after reading in 16, and converts to the ASCII code VF.

Line seventh: Pack ("H3", 124); Because the byte is to be fully replenished first, the full complement is 1240. 1240 is the decimal (h default is read read decimal, read in 16), so 1240 was hit by H is 0x1240, converted to 2 in the words on the 0001 0010 0100 0000, converted to 16 is 0x1240, and the browser here is read in ASCII code, so it is a 8-bit translation, so 0x1240 is divided into 0x12 and 0x40,0x40 is @,0x12 in the ASCII table can be seen is (Device Control 2) device Controls 2, This thing is displayed in the IE8 is the up and down double-headed arrows. In Ie6,ie7 browsers, different characters are displayed because of the different encoding methods used.

Line eighth: Pack ("H3", 124) because H is low in front, and it is 1 half bytes (remember.) The byte must be fully replenished before H is to be used for the pack method, and then we'll compare H and H,
Before reading hexadecimal 0x7c
Read before decimal 1240
H read-after hexadecimal 0x1240
H read-after hexadecimal 0x0421
H read after the binary 0001001001000000 high in front of a single H-read object based, 4-bit as the unit, to exchange the high-low
H read the binary 0000010000100001 low before the first time with a H-read object as the basis, 4-bit as the unit, the high and low interchange

0000 0100 0010 0001 or 0x0421, divided into 0x04 and 0x21, expressed in ASCII code in IE8 as shown above


=================v and N====v and n=======================
-------------------------V-n-----------------------
First look at Chinese translation
V unsigned short integers (32-bit, low in the back order)
N short-unsigned integers (32-bit, high in the latter order)


Read English again
V--unsigned long (always bit, little endian byte order) unsigned length integer (Always 32 bits, low address storage Minimum valid byte) (according to PowerWord explanation)
N--unsigned long (always bit, big endian byte) unsigned length shaping (always 32-bit, high address storage Minimum valid byte) (according to PowerWord explanation)


The following explains little endian and big endian
Big endian--refers to a low address for storing the highest valid byte
Little Endian--refers to a low address for storing the least significant byte

For example: a = 0x05060708
0x05060708
High-end <---Low-end

stored in the case of Big-endian:
Because low-end addresses store high-end words, 08 is the lowest valid word placed at the high-end address
BYTE number 0 1 2 3 ···
Low-end address high-end address
----------------------------------------->
Data 05 06 07 08 ···


stored in the case of Little-endian:
Because the low address holds the least effective byte, 08 is the lowest valid word placed at the low-end address
BYTE number 0 1 2 3 ···
Low-end address high-end address
----------------------------------------->
Data 08 07 06 05 ···

Because memory is read from the low-end address, because read the memory data generally use the address pointer, read an address in addition to an offset. So this memory Little-endian and Big-endian determine the order of the data to read, not to the above the Chinese translation of the son (what low in the order of the latter, I was served, translated really let me Out).


Here are a few examples of V
echo "1". Pack ("V", 0x65666768696a6b); echo "<br/>";
echo "2". Pack ("V", 0x666768696a6b6c); echo "<br/>";
echo "3". Pack ("V", 0x6768696a6b6c6d); echo "<br/>";

Show up for
Kjih 01101011 01101010 01101001 01101000
K J I H
Lkji 01101100 01101011 01101010 01101001
L K J I
MLKJ 01101101 01101100 01101011 01101010
M L K J

Example of n
echo "4". Pack ("N", 0x65666768696a6b); echo "<br/>";
echo "5". Pack ("N", 0x666768696a6b6c); echo "<br/>";
echo "6". Pack ("N", 0x6768696a6b6c6d); echo "<br/>";

Show up for
Hijk 01101000 01101001 01101010 01101011
H i j K
IJKL 01101001 01101010 01101011 01101100
I j k L
JKLM 01101010 01101011 01101100 01101101
J k L M

-------------------V-n----------------
First look at Chinese translation
V unsigned short integers (16-bit, low in the back order)
n Short-unsigned integers (16-bit, high in the latter order)


Read English again
V--unsigned short (always bit, little endian byte) unsigned integer (always 16 bits, low address storage Minimum valid byte) (according to PowerWord explanation)
N--unsigned short (always bit, big endian byte) (always 16 bits, high address storage, minimum valid byte) (according to Kingsoft)


Needless to say, the difference between V and N is 16-bit and 32-bit.


-------------------about the problem of v-n overflow--------------------------


================ a ===================
First look at Chinese translation
A a filled-empty byte string

Look at English translation again

A nul-padded string
A nul-padded string
A nul-padded string

Here is an example:
Echo Pack ("a", 65). " <br/> ";
Echo Pack ("A2", 65). " <br/> ";
Echo Pack ("A2", 65,66). " <br/> ";
Echo Pack ("A2A2", 65,66). " <br/> ";
Echo Pack ("a", "65"). " <br/> ";
Echo Pack ("A2", "65"). " <br/> ";
Echo Pack ("a", 0x65). " <br/> ";
Echo Pack ("A1", 0x65). " <br/> ";
Echo Pack ("A2", 0x65). " <br/> ";
Echo Pack ("A3", 0x65). " <br/> ";

The output is:
6
65
Error
65
6
65
1
1
10
101

From the example above, this damned a, is read in decimal way, whether the character or hexadecimal is first converted to decimal read, A1 is to read a decimal (also do not know how many bits), A2 is read two decimal (also do not know how many bits), A3 is read three-bit decimal (also do not know how many bits). If it is hexadecimal, the first conversion to decimal, such as the above 0X65 converted to 101, and then read with A3 after the display is 101.

-------------------------------------------------------------------------------------------------------

Echo Pack ("CCCC", 0x41, 0x42, 0x43, 0x44);        //ABCD
Echo Pack (" CCCC ",;               , A.        //ABCD
Echo Pack ("C4", 0x41, 0x42, 0x43, 0x44);            //ABCD
Echo Pack ("C4",     &,,); nbsp;                     //ABCD

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.