PHP pack () functions in a detailed and example

Source: Internet
Author: User
Tags unpack vars

Pack and unpack are not really easy to see in normal programs, but if you use PHP from a long time ago to generate Excel you will know. The head of his Excel is the pack.
Recently, when I tried to interact with C, I used this thing again, so I had to look at it again. Actually is C request I must have Baotou ... In fact, the pure string is also good. Why do you have to do a Baotou? Really tangled.

The manual has an introduction to pack and unpack, but it's all in English ...

Any language that has the ability to socket can have a function specifically for the group package, PHP is no exception!

It took a long time for PHP to have a few binary operations with PHP. Recently wrote a socket client with PHP to connect a game server developed in C + + language. The server-side developer uses a binary form to define the format of the protocol. The protocol format is as follows:

Baotou (2bytes) + encryption (1byte) + command Code (2bytes) + frame content

1. Baotou content is the length of the recording frame content;
2. Encryption: 0 means no encryption, 1 means encryption;
3. Command code for service-side command identification symbols;

Initially do not understand PHP originally have pack can come to assemble binary package, took a detour, let the server developer in C language help develop several memory operation function, follow Protocol rule return binary package, then I will compile these methods into a set of extension function for PHP to use.

To the point, this article describes how to use both the pack and unpack methods. The official PHP manuals are too few to be easily understood, especially for the use of formatting parameters.

The parameters of the transfer in Chinese description:

Pack/unpack character meaning of the plate
A a filled empty byte string
A a byte string that fills a space
b A bit string, in which the order of bits in each byte is ascending
B a bit string, in which the order of bits in each byte is descending
c A signed char (8-bit integer) value
C an unsigned char (8-bit integer) value; about Unicode See U
D double-precision floating-point number in native format
F single-precision floating-point number in native format
h a hexadecimal string, low four bits in front
H a hexadecimal string, high four bits in front
I a signed integer value, native format
I an unsigned integer value, native format
L A signed long shaping, always 32 bits
L An unsigned long shaping, always 32 bits
N A 16-bit short shaping, "Network" byte order (head in front)
N a 32-bit short shaping, "Network" byte order (head in front)
p A pointer to a string with a null end
P A pointer to a fixed-length string
Q A signed four times-fold (64-bit integer) value
Q an unsigned four times-fold (64-bit integer) value
s a signed short integer value, always 16 bits
S an unsigned short integer value, always 16 bits, byte sequence related to the machine chip
U a non-encoded string
U a Unicode character number
V A "VAX" byte sequence (small head in front) of a 16-bit short integer
V a "VAX" byte sequence (small head in front) of a 32-bit short integer
W a BER-compressed integer
X an empty byte (ignoring one byte forward)
X Backup of one byte
Z a null-terminated (and empty-filled) byte string
@ Fill absolute position with empty bytes


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

Some rules:
1. Each letter can be followed by a number, indicating count (count), if Count is a * represents the rest of everything.
2. If you provide fewer parameters than $format requirements, the pack assumes that the missing is a null value. If you provide more parameters than $format require, the extra parameters are ignored.

The following is an example of how a usage can be easily understood: PHP code
  1. About Pack:
  2. The first part of the following wraps the numeric value into bytes:
  3. $out = Pack ("CCCC", 65, 66, 67, 68); # $out equals "ABCD"
  4. $out = Pack ("C4", 65, 66, 67, 68); # The same thing
  5. The following is the same thing for the Unicode circular letters:
  6. $foo = Pack ("U4", 0x24b6, 0x24b7, 0x24b8, 0x24b9);
  7. The following do something similar, adding some empty:
  8. $out = Pack ("CCXXCC", 65, 66, 67, 68); # $out equals "AB\0\0CD"
  9. Packing your short integers does not mean you can migrate:
  10. $out = Pack ("S2", 1, 2);
  11. # on the front of the machine is the "\1\0\2\0"
  12. # on the front of the machine is "\0\1\0\2"
  13. On binary and hex wrappers,count refers to the number of bits or half bytes, not the number of bytes generated:
  14. $out = Pack ("B32", "...");
  15. $out = Pack ("H8", "5065726c"); # all generate "Perl"
  16. The length in a field applies only to one string:
  17. $out = Pack ("A4", "ABCD", "x", "y", "Z"); # "ABCD"
  18. To bypass this restriction, use multiple-fold declarations:
  19. $out = Pack ("AAAA", "ABCD", "x", "y", "Z"); # "axyz"
  20. $out = Pack ("a" x 4, "ABCD", "x", "y", "Z"); # "axyz"
  21. A-format short padding:
  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. Finally, the protocol at the beginning of this article uses the Pack/unpack Example program code:
  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

I have the above content from: http://blog.sina.com.cn/s/blog_3eba8f1c0100nq9r.html, I have now successfully used the finished. Black Black
Other references:
Http://bbs.phpchina.com/thread-104492-1-1.html
Http://hi.baidu.com/chinetman/item/f78a71d847e7d638e2108fda

PHP pack () functions in a detailed and example

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.