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
- About Pack:
- The first part of the following wraps the numeric value into bytes:
- $out = Pack ("CCCC", 65, 66, 67, 68); # $out equals "ABCD"
- $out = Pack ("C4", 65, 66, 67, 68); # The same thing
- The following is the same thing for the Unicode circular letters:
- $foo = Pack ("U4", 0x24b6, 0x24b7, 0x24b8, 0x24b9);
- The following do something similar, adding some empty:
- $out = Pack ("CCXXCC", 65, 66, 67, 68); # $out equals "AB\0\0CD"
- Packing your short integers does not mean you can migrate:
- $out = Pack ("S2", 1, 2);
- # on the front of the machine is the "\1\0\2\0"
- # on the front of the machine is "\0\1\0\2"
- On binary and hex wrappers,count refers to the number of bits or half bytes, not the number of bytes generated:
- $out = Pack ("B32", "...");
- $out = Pack ("H8", "5065726c"); # all generate "Perl"
- The length in a field applies only to one string:
- $out = Pack ("A4", "ABCD", "x", "y", "Z"); # "ABCD"
- To bypass this restriction, use multiple-fold declarations:
- $out = Pack ("AAAA", "ABCD", "x", "y", "Z"); # "axyz"
- $out = Pack ("a" x 4, "ABCD", "x", "y", "Z"); # "axyz"
- A-format short padding:
- $out = Pack ("A14", "ABCDEFG"); # "abcdefg\0\0\0\0\0\0"
- About Unpack:
- Array unpack (String $format, String $data)
- $data = "010000020007";
- Unpack ("Sint1/cchar1/sint2/cchar2",$data);
- # # Array (' int1 ' =>1, ' char1 ' = ' 0 ',' Int2 ' =>2,' char2 ' =>7);
- Finally, the protocol at the beginning of this article uses the Pack/unpack Example program code:
- $lastact = Pack (' Scsa32a32 ', 0x0040, 0x00, 0x0006, $username, $passwd);
- 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