Phppack () function description and example

Source: Internet
Author: User
Tags in domain
: This article mainly introduces the phppack () function details and examples. if you are interested in the PHP Tutorial, refer to it. Pack and unpack are not easy to see in general programs, but if you have used php to generate excel for a long time ago, you will know. His excel header is packed.
I recently used this item when I tried to interact with C, so I have to check it out again. In fact, C requires that I have a Baotou... In fact, pure strings are also good. Why do we have to build a Baotou? Really tangled ..
The manual includes the introduction of pack and unpack, but both are in English...

Any language with socket operation capability has a function dedicated to group packages, and php is no exception!
Php has been used for a long time, but has little chance to use php for some binary operations. Recently, I used php to write a socket client to connect to a game server developed in C ++. Server developers use a binary format to define the protocol format. The protocol format is as follows:
Header (2 bytes) + encrypted (1 byte) + imperative code (2 bytes) + frame content
1. the header content is the length of the recorded frame content;
2. Encryption: 0 indicates not encrypted; 1 indicates encryption;
3. the command code is the server-side command recognition symbol;
At the beginning, I didn't know that php had a pack that could be used to assemble binary packages. this took a detour and asked the server developers to use the C language to help develop several memory operation functions and return binary packages according to protocol rules, then I compiled these methods into a set of extension functions for php.

This article describes how to use the pack and unpack methods. There are too few official php manual examples to be easily understood, especially for formatting parameters.
Description of the parameters of the secondary node:
Character meaning of pack/unpack
A is a null byte string.
A is A byte string filled with spaces.
B is a single-byte string in ascending order.
B is a single-byte string. the bits in each byte are sorted in descending order.
C is a signed char (8-digit integer) value.
C is an unsigned char (8-digit integer) value. for Unicode, see U
D. double-precision floating point number in local format
F single-precision floating point number in local format
H: a hexadecimal string with four lower digits in front
H: a hexadecimal string with four digits in front
I a signed integer in the local format
I an unsigned integer in the local format
L a signed long integer, always 32-bit
L An unsigned long integer, always 32-bit
N a 16-bit short integer, "network" in byte order (front of the big head)
N a 32-bit short integer, "network" in byte order (front of the big head)
P a pointer to a null string
P a pointer to a fixed-length string
Q: A signed value of four times (64-bit integer)
Q: an unsigned four times (64-bit integer) value
S a signed Short integer value, always 16 bits
S is an unsigned short integer value, which is always 16 bits. the byte order is related to the machine chip.
U a non-encoded string
U a Unicode character number
V is a 16-bit short integer in the byte order of "VAX" (before the small header ).
V is a 32-bit short integer in the byte order of "VAX" (before the small header ).
W: an integer compressed by a BER
X is an empty byte (ignore one byte forward)
X back up one byte
Z: An empty ending (and an empty-filled) byte string
@ Fill in the absolute position with null bytes
String pack (string $ format [, mixed $ args [, mixed $...])
Some rules:
1. each letter can be followed by a number, indicating count. if count is a *, it indicates all the remaining items.
2. if you provide fewer parameters than $ format, pack assumes that all parameters are missing are null values. If you provide more parameters than $ format, extra parameters are ignored.
The following is an example to illustrate the usage:

PHP code

  1. About Pack:
  2. The first part below wraps the numeric value into bytes:
  3. $ Out = pack ("CCCC", 65, 66, 67, 68); # $ out equals to "ABCD"
  4. $ Out = pack ("C4", 65, 66, 67, 68); # The same thing
  5. The following example shows how to do the same thing for Unicode circular characters:
  6. $ Foo = pack ("U4", 0x24b6, 0x24b7, 0x24b8, 0x24b9 );
  7. In the following example, some null values are added:
  8. $ Out = pack ("CCxxCC", 65, 66, 67, 68); # $ out equals to "AB \ 0 \ 0CD"
  9. Packing your short integer does not mean you can transplant it:
  10. $ Out = pack ("s2", 1, 2 );
  11. # "\ 1 \ 0 \ 2 \ 0" on the machine with a small header"
  12. # "\ 0 \ 1 \ 0 \ 2"
  13. In binary and hexadecimal packaging, count refers to the number of bits or bits, rather than the number of generated bytes:
  14. $ Out = pack ("B32 ","...");
  15. $ Out = pack ("H8", "5065726c"); # generate "Perl"
  16. The length in domain a is only applied to one string:
  17. $ Out = pack ("a4", "abcd", "x", "y", "z"); # "abcd"
  18. To bypass this restriction, use the multi-factor statement:
  19. $ Out = pack ("aaaa", "abcd", "x", "y", "z"); # "axyz"
  20. $ Out = pack ("a" x 4, "abcd", "x", "y", "z"); # "axyz"
  21. Short fill in a format:
  22. $ Out = pack ("a14", "abcdefg"); # "abcdefg \ 0 \ 0 \ 0 \ 0 \ 0 \ 0"
  23. About unpack:
  24. Array unpack (string $ format, string $ data)
  25. $ Data = "010000020007 ";
  26. Unpack ("Sint1/Cchar1/Sint2/Cchar2", $ data );
  27. # Array ('int1' => 1, 'char1' => '0', 'int2' => 2, 'char2' => 7 );
  28. The sample code of the protocol pack/unpack mentioned at the beginning of this article is as follows:
  29. $ Lastact = pack ('scsa32a32', 0x0040, 0x00, 0x0006, $ username, $ passwd );
  30. Unpack ('sint1/Cchar1/Sint2/Cchar2/', $ lastmessage );


Learning materials:
Http://blog.csdn.net/jojobb3138688/archive/2007/05/07/1598609.aspx
My above content comes from: callback. Black and Black
Other references:
Http://bbs.phpchina.com/thread-104492-1-1.html
Http://hi.baidu.com/chinetman/item/f78a71d847e7d638e2108fda

The above introduces the php pack () function details and examples, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.

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.