Use of pack and unpack

Source: Internet
Author: User
Tags ord unpack
This time to everyone to bring pack, unpack use of detailed, use pack, unpack note what, the following is the actual case, together to see.

There are two function packs and unpack in PHP, and many phper have never been used in actual projects, or even know what these two methods are intended to do. This article will introduce you to what they are used to do.

Pack

String Pack (String $format [, Mixed $args [, mixed $ ...])

This function is used to package the corresponding parameter ($args) into a binary string.

The first parameter, $format, has the following options (there are a number of optional parameters, which will be followed by a few common explanations):

Code Description
A Padding string blanks with nul bytes
A Fills a string with space (space)
H hexadecimal string, low in front
H hexadecimal string, high in front
C Signed characters
C unsigned characters
S Signed short integer (16-bit, host byte-order)
S unsigned short integer (16-bit, host byte-order)
N unsigned short integer (16-bit, big-endian byte-order)
V unsigned short integer (16-bit, small-endian byte-order)
I Signed integer (machine-Relative size byte-order)
I unsigned integer (machine-Relative size byte-order)
L Signed Long Integer (32-bit, host byte-order)
L unsigned long integer (32-bit, host byte-order)
N unsigned long integer (32-bit, big-endian byte-order)
V unsigned long integer (32-bit, small-endian byte-order)
Q Signed Long Integer (64-bit, host byte-order)
Q unsigned long integer (64-bit, host byte-order)
J unsigned long integer (64-bit, big-endian byte-order)
P unsigned long-length integer (64-bit, small-endian)
F Single-precision floating-point type (machine-relative size)
D Double-precision floating-point type (machine-related size)
X Nul bytes
X Fallback one byte
Z Padding string blanks with nul bytes (new in PHP 5.5)
@ Nul fill to absolute position

So many parameters to see, I first was really crazy, most of the instructions are very good understanding, but the host, big, small end, such as the byte sequence is what ghosts? The content of the next is more boring, but must understand, stick to it.

What is the byte order?

is the order of the bytes, which is plainly the order in which multibyte data is stored (one byte clearly does not need a sequence).

For example, A and B correspond to the binary representation of 0100 0001, 0100 0010. For the storage string AB, we can 0100 0001 0100 0010 can also be 0100 0010 0100 0001, which is called the byte order.

High/Low Byte

such as the string AB, Zogao right (our normal reading order), A is high byte, B is low byte

High/Low Address

Suppose that the 0x123456 is stored in the order from the high to the bottom, in memory:

High address-low address
56

Endian byte order (network byte order)

The big end is to place the high byte to the low address of the memory, and the low byte to the high address. The low address end of a network transmission (such as TCP/IP) is placed at the beginning of the stream, and for a 2-byte string (AB), the transmission order is: A (0-7bit), B (8-15bit).

Then the small-endian byte order is natural and big-endian opposite.

Host byte order

Indicates the byte order of the current year (that is, the network byte order is determined, and the host byte order is determined by the machine), usually the small-endian byte order.

A and a (packing strings, filled with nul or spaces)

$string = Pack (' A6 ', ' China '); Var_dump ($string); Output: String (6) "China", the last byte is Invisible Nulecho ord ($string [5]); Output: 0 (ASCII code in 0 corresponds to nul)//a $string = Pack (' A6 ', ' China '); Var_dump ($string); Output: String (6) "China", the last byte is the Space echo Ord ($string [5]); Output: 32 (ASCII code in the corresponding space)

One Piece of ASCII table (Linux/unix can be viewed with man ASCII)

H and H

$string = Pack (' H3 ', 281); Var_dump ($string); Output: String (2) "(" for ($i =0; $i <strlen ($string); $i + +) {echo ord ($string [$i]). Php_eol;} Output results: 40 16

H and H require special instructions, which are to consider the corresponding parameters as hexadecimal characters and then package them. What do you mean? For example, the above 281, before packaging will convert 281 to 0x281, because the hexadecimal one corresponding to the binary four-bit, the above 0x281 only 1.5 bytes, and then the default is 0 to 0x2810,0x28 corresponding decimal ((), 0x10 corresponding decimal 16 ( Dle invisible characters), do you understand? Do not know can give me a message.

C and C

$string = Pack (' C3 ', Var_dump, 1); ($string); Output: String (3) "CD" for ($i =0; $i <strlen ($string); $i + +) {echo ord ($string [$i]). Php_eol;} Output: 67 68 225

The final output instinct should feel like 67 68-1

Ord gets the ASCII code of the character (range 0-255), at which point-1 (0000 0001) The corresponding character will be output in the form of a complement of 255 (1111 1110 + 0000 0001 = 1111 1111)

Integral type related

All integral types are used exactly the same way, with the main attention to their bit and byte order, as shown in L as an example

$string = Pack (' L ', 123456789); Var_dump ($string); Output: String (4) "[" for ($i =0; $i <strlen ($string); $i + +) {echo ord ($string [$i]). Php_eol;} Output: 21 205 91 7

F and D

$string = Pack (' F ', 12345.123); Var_dump ($string);//Output: String (4) "~ @F" var_dump (Unpack (' F ', $string)); The unpack is used in advance, the following will explain//output: float (12345.123046875)

F and D is for floating-point packaging, as for why packaging is 12345.123 after unpacking is 12345.123046875, which is related to the storage of floating-point number, you can open a single article on the IEEE standard

x, X, Z, @

$string = Pack (' x '); Pack a NUL string echo ord ($string); Output: 0

About X (capital X), tried n times, did not understand how to use, there are clear children's shoes can give me a message, thank you.

$string = Pack (' Z2 ', ' Abc5 '); In fact, it will start from the digital position behind Z, all set to Nulvar_dump ($string); Output: String (2) "A" for ($i =0; $i <strlen ($string); $i + +) {echo ord ($string [$i]). Php_eol;} Output: 97 0
$string = Pack (' @4 '); I understand to fill n nulvar_dump ($string); Output: String (4) "" for ($i =0; $i <strlen ($string); $i + +) {echo ord ($string [$i]). Php_eol;} Output: 0 0 0 0

Unpack

Array unpack (string $format, String $data)

The use of unpack is quite simple, that is, pack packaging data unpacking, packaging, what parameters to use what parameters to unpack, specific use lazy said, a few small examples

$string = Pack (' L4 ', 1, 2, 3, 4); Var_dump (Unpack (' L4 ', $string));//Output: Array (4) {[1]=>int (1) [2]=>int (2) [3]=> int (3) [4]=>int (4)} $string = Pack (' L4 ', 1, 2, 3, 4); Var_dump (Unpack (' Ll1/ll2/ll3/ll4 ', $string)); You can specify key, use/split//Output: Array (4) {["L1"]=>int (1) ["L2"]=>int (2) ["L3"]=>int (3) ["L4"]=>int (4)}

What is the purpose of these two functions?

    1. Data communication (communication with other languages in binary format)

    2. Data encryption (if you don't tell a third party how hard it is to pack, it's a lot harder to unpack)

    3. Space saving (for example, larger numbers can waste a lot of space when stored in a string, and packaging in binary format requires 4-bit <32 digits >)

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

How PHP generates random numbers

Post cannot get form data

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.