Struct in python is mainly used to process C-structure data. it is first converted to a Python string type during reading, and then to a Python structured type, such as a tuple ~. Generally, the input channel comes from the binary stream of a file or network. Struct in python is mainly used to process C-structure data. it is first converted to a Python string type during reading, and then to a Python structured type, such as a tuple ~. Generally, the input channel comes from the binary stream of a file or network.
1. struct. pack () and struct. unpack ()
In the conversion process, a format strings is used to specify the conversion method and format.
The following describes the main methods:
1.1 struct. pack (fmt, v1, v2 ,.....)
Package the values of parameters such as v1 and v2. the packaging method is specified by fmt. The encapsulated parameters must strictly comply with fmt. Returns a wrapped string.
1.2 struct. unpack (fmt, string)
Take care of your name and unpack the package. For example, pack the package and unpack the package. Returns a tuple obtained from the unwrapped data (string). even if only one data is returned, it is unwrapped into a metagroup. Len (string) must be equal to calcsize (fmt), which involves a calcsize function. Struct. calcsize (fmt): This is used to calculate the size of the structure described in the fmt format.
A format string is composed of one or more format characters (format characters ).
Format |
C Type |
Python |
Note |
X |
Pad byte |
No value |
|
C |
Char |
String of length 1 |
|
B |
Signedchar |
Integer |
|
B |
Unsignedchar |
Integer |
|
? |
_ Bool |
Bool |
(1) |
H |
Short |
Integer |
|
H |
Unsignedshort |
Integer |
|
I |
Int |
Integer |
|
I |
Unsignedint |
Integer or long |
|
L |
Long |
Integer |
|
L |
Unsignedlong |
Long |
|
Q |
Longlong |
Long |
(2) |
Q |
Unsignedlonglong |
Long |
(2) |
F |
Float |
Float |
|
D |
Double |
Float |
|
S |
Char [] |
String |
|
P |
Char [] |
String |
|
P |
Void * |
Long |
|
2. Sample code
import struct # native byteorder buffer = struct.pack("ihb", 1, 2, 3) print repr(buffer) print struct.unpack("ihb", buffer) # data from a sequence, network byteorder data = [1, 2, 3] buffer = struct.pack("!ihb", *data)print repr(buffer) print struct.unpack("!ihb", buffer) Output:'\x01\x00\x00\x00\x02\x00\x03'(1, 2, 3)'\x00\x00\x00\x01\x00\x02\x03'(1, 2, 3)
View Code
First, package the parameters 1, 2, 3, and the first 1, 2, and 3 are obviously integers in the python data type. pack the data into a binary string of the C structure, convert the string type to '\ x01 \ x00 \ x00 \ x00 \ x02 \ x00 \ x03 '. Because the local machine is a small terminal ('little-endian '), please refer to the differences between the big end and the small end here, so the high point is placed in the low address segment. I represents the int type in C struct, so the local machine occupies 4 places, 1 represents 01000000; h represents the short type in C struct, occupies 2 places, so it represents 0200; similarly, B represents the signed char type in C struct, which occupies 1 and thus represents 03.
The conversion of other structures is similar. for some special information, see Manual in the official document.
At the top of the Format string, there is an optional character to determine the big end and small end, the list is as follows:
|
|
|
@ |
Native |
Native |
= |
Native |
Standard |
< |
Little-endian |
Standard |
> |
Big-endian |
Standard |
! |
Network (= big-endian) |
Standard |
If no append exists, the default value is @, that is, the local character sequence (large or small) is used. The size of the C structure and the alignment in the memory are also consistent with those in the local machine (native ), for example, some machines have two integers, while some machines have four digits. Some machines have four alignment for memory, while others are n-bit alignment (n unknown, I don't know how much ).
There is also a standard option, which is described as: if the standard option is used, no memory alignment is available for any type.
For example, in the second half of the applet, the first digit in the format string used is !, That is, the standard alignment mode of the big end mode. Therefore, the output is '\ x00 \ x00 \ x00 \ x01 \ x00 \ x02 \ x03 ', in this case, you can put the high address in the memory.
For more information about the struct. pack () and struct. unpack () functions in python, see The PHP Chinese web!