Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Ask dos experts to see how to use this software.
Ask dos experts to see how to use this software.
Any language with socket operation ability has functions specially used for package assembly, and php is no exception! I've been using php for a long time, but I rarely get a chance to do some binary operations with php. Recently, I wrote a socket client in php to connect with a game server developed in C++ language. Server-side developers use binary form to define the format of the protocol. The protocol format is as follows: header (2 bytes)+encryption (1 byte)+command code (2 bytes)+frame content 1. The content of the header is the length of the recorded frame content;

2. Encryption: 0 means no encryption, and 1 means encryption;

3. The command code is the server command identification symbol; At first, I didn't know that php originally had a pack to assemble binary packages, so I asked the server developer to help develop several memory operation functions in C language and return the binary packages according to the protocol rules. Then I compiled these methods into a set of extension functions for php to use. Explanation:

The function package template LIST receives a list of common Perl values, converts them into a byte string according to the template, and then returns the string. The parameter list will be filled in or cut when necessary. In other words, if you provide fewer parameters than the template requires, pack will assume that all missing parameters are null. If the parameters you provide exceed the requirements of the template, the extra parameters will be ignored. Unrecognized format elements in the template will throw an exception. The template describes the structure of a string as a domain sequence. Each field is represented by a character that describes the type of the value and its encoding. For example, the format character n declares a four-byte unsigned integer with its big head in front. Fields are wrapped in the order given in the template. For example, if you want to wrap a single-byte unsigned integer and a single-precision floating-point value into a string, you can say:

$string = pack("CF ",244,3. 14); The value of the first byte of the returned string is 244. The remaining bytes are 3. 14, which is used as the encoding of single-precision floating-point numbers. The specific encoding of floating-point numbers depends on the hardware of the computer. Some important things to consider when packaging are:

The type of data (such as integer, floating point or string),

The range of values (for example, your integer is one, two, four, or even eight bytes; Or packing 8-bit characters or Unicode characters. ),

Whether your integer is signed or unsigned, and

The encoding used (such as this machine, when packing bits and bytes, the small head comes first, or the big head comes first).

In a word, this paper introduces how to use the two methods of packaging and unpacking. There are too few examples in the official php manual to understand, especially the use of formatting parameters. Character meaning of packaged/unpacked touch panel characters

A byte string filled with spaces.

A byte string filled with spaces.

A string of bits in which the order of bits in each byte is ascending.

A string of bits in which the order of bits in each byte is decreasing.

A signed character (8-bit integer) value.

C unsigned character (8-bit integer) value; See u for Unicode.

Double-precision floating-point number in D native format

F single-precision floating-point number in native format

H is a hexadecimal string with the lower four digits first.

H is a hexadecimal string, with four high digits first.

I signed integer value, native format.

I unsigned integer value, native format.

L a signed long integer, always 32 bits.

L unsigned long integer, always 32 bits.

N A 16-bit short plastic with "network" byte order (big head first)

N A 32-bit short integer with "network" byte order (big head first)

Pointer to an empty termination string.

Pointer to a fixed-length string.

A signed quad (64-bit integer) value.

An unsigned quad (64-bit integer) value.

S is a signed short integer value, always 16 bits.

S is an unsigned short integer value, always 16 bits, and the byte order is related to the machine chip.

U unencoded string.

U number of Unicode characters

A 16-bit short integer with "VAX" byte order (small header first).

A 32-bit short integer with "VAX" byte order (small header first).

A BER-compressed integer

X an empty byte (ignoring the previous byte)

X backs up one byte.

Z a null-terminated (and null-padded) byte string

@ Fill the absolute position with empty bytes

You are free to use spaces and comments in the template. Comments begin with the usual # symbol and extend to the first line break in the template (if it exists). Each letter can be followed by a number to indicate the count, which can be interpreted as some form of repeated count or length, depending on the format. All formats except a, a, b, b, h, h, p, z and count are repeated, so pack ate so many values from the list. If count is a *, it means everything else. The format A, A and Z only takes a numerical value, but it is encapsulated into a byte string with the length of count, and spaces are filled as needed. When unpacking, A deletes the spaces and blanks at the end, Z deletes everything after the first blank, and A returns the text data intact. When packing, A and Z are the same. Similarly, the B and B format encapsulates a bit string of length count. Each byte in the input field generates 1 bit in the result according to the lowest bit of each input byte (that is, ord($byte)% 2). Conveniently, this means that byte 0 and 1 produce bits 0 and 1. From the beginning of the input string, every 8 bytes are converted into one byte of output. If the length of the input string is not divisible by 8, then the rest is filled with 0. Similarly, any extra bits are ignored during the uppack. If the input string is longer than required, the extra part will be ignored. If count is *, it means that all bytes of the input field are used. When unpacking, these bits are converted into a string consisting of 0 and 1 H and H formats encapsulate many counting nibbles (4-bit groups, usually used to represent hexadecimal bits. ) is a string composed of. The p format encapsulates a string pointing to an empty end. It is your responsibility to ensure that the string is not a temporary value (because temporary values may be released before you can use the packaged results). The p format encapsulates a pointer to a structure whose size is represented by count. If the corresponding value of p or p is undef, a null pointer is created. Characters allow strings to be packed or unpacked: the packed structure contains many bytes followed by the string itself. You can write length-item/string-item like this. Length-item can be any package template character that describes how the length value is packaged. Probably the most commonly used is integer packing, such as n (for packing Java strings), w (for packing ASN. 1 or SNMP) and n (for Sun XDR). String-item must be A*, a* or Z* at present. For unpack, the length of the string is obtained from the length-item, but if you put an * sign in it, it will be ignored. sample

Unpacking' C/a', "\ 04 gurusamy ";; # Generate "Master"

Uppack 'a3/A* A*',' 077 Bond J ';; # Generate ('Bond ',' J ')

Pack' n/a* w/a*',' hell',',' world'; # Generate "Hello, World"

Length items are not explicitly returned from unpacking. Adding a count to a length-item letter may not be useful unless the letter is a, a or z. Packing a or z with length-item may introduce null (\0) character, in which case Perl will think it is not a legal numeric string. Integer format s, s, l, l can be followed by one! , representing local short integers or long integers, respectively, instead of the exact 16 bits and 32 bits. Now, this is a problem on many 64-bit platforms, and the local C compiler sees that the native short integer and long integer may be different from the above values. (me! And me! Can also be used, but only to maintain integrity; They are the same as I and I. You can get the actual lengths of native short, int, long and long long through the Config module: samples on the platform of making your Perl.

Use configuration;

Print $ $Config{shortsize}, "\ n ";;

Print $Config{intsize}, "\ n ";;

Print $Config{longsize}, "\ n ";;

Print $ config {longsize}, "\ n ";;

It just means that Configure knows the size of a long long, but it doesn't mean that you can use q and q, some systems can, and the system you use probably doesn't. Integer formats (S, S, I, I, L and L) with a length greater than one byte cannot be transplanted between different processors in essence, because they must follow the rules of native byte order and bit weight order. If portable integers are needed, use N, N, V and V formats; Because they are sorted by byte weight and their sizes are known. Floating-point numbers exist only in native format. There is no floating-point exchange tool because floating-point formats vary widely and there is no standard "network" expression. This means that floating-point data packaged on one machine may not be readable on another machine. Even if both machines use IEEE floating-point operations, this is still a problem because the memory representation related to weights is not part of the IEEE specification. Perl uses double-precision numbers internally for all floating-point calculations, so converting from double to float and then to float will lose precision. This means that unpack("f ",pack("f", $foo)) may not be equal to $foo. It is your responsibility to consider the alignment or padding of other programs, especially those with the heterogeneous concept of C compiler. There are great differences in how C compiler lays out C struct under different architectures. You may need to add enough x when packing to make up for this problem. For example: a C statement

Structural foo {

Unsigned character c;

Floating f;

}; It can be written in "C x f" format, "C x3 f" format, or even "f C" format-and this is only part of it. Packaging and unpacking functions treat their input and output as a simple sequence of bytes because they don't know where these bytes go or where they come from. PACK function string package (string $ format [,mixed $ args [,mixed $...]]) Some rules:

1. Each letter can be followed by a number to indicate the count. If count is a *, it means everything else.

2. If you provide fewer parameters than required by $format, then pack will think that all the missing parameters are null. If you provide more parameters than required by $format, the extra parameters will be ignored. Let's give an example to illustrate the usage, which will be easier to understand: about Pack: the first part below wraps the digital value into bytes:

$out = pack("CCCC ",65,66,67,68); # $out equals "ABCD"

$out = pack("C4 ",65,66,67,68); # Same thing

The following code does the same thing for Unicode circular letters:

$foo = pack("U4 ",0x24b6,0x24b7,0x24b8,0x24b 9);

Let's do something similar and add some space:

$out = pack("CCxxCC ",65,66,67,68); # $out equals "AB\0\0CD"

Packaging your short integers does not mean that you are portable: $out = pack("s2 ",1, 2);

# On a machine with a small head in front, it is "1 \ 0 \ 2 \ 0"

# On the machine with a big head in front, it is "\0\ 1\0\2"

In binary and hexadecimal packets, counting refers to the number of bits or nibbles, not the number of bytes generated:

$out = pack("B32 ","...");

$out = pack("H8 "," 5065726 c "); # Generate "perl". The length in the a field applies only to strings:

$out = pack("a4 "、" abcd "、" x "、" y "、" z "); # "abcd "To get around this restriction, please use multiple 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("a 14 "," abcdefg "); # " abcdefg\0\0\0\0\0 "

About unpack: array unpack (string $ format, string $ data) $ data = "010000020007";

unpack(" Sint 1/cchar 1/Sint 2/cchar 2 ",$ data); # # array ('int1'= > 1,' char 1'= >0 ',' int2 ' = & gt2,' char2 ' = & gt7); Finally, the sample program code using the packing/unpacking protocol mentioned at the beginning of this article is:

$ lastac = pack(' scsa 32 a 32 ',0x0040,0x00,0x0006,$username,$ passwd);

Unpack ('Sint1/CCHAR1/Sint 2/CCHAR2/',$ lastmessage This article is from the CSDN blog, please indicate the source:/encourage007/archive/2008/114/.