Python is a very concise language, and its representation of data types is not as predefined in other languages (for example, in C #, 8 types of optical integers are defined ), it only defines six basic types: String, integer, floating point, tuples, lists, and dictionaries. With these six data types, We can do most of our work. However, when Python needs to interact with other platforms through the network, it is necessary to consider the issue of converting these data types with those of other platforms or languages. For example, a C ++ client sends an int-type (4-byte) variable data to a python-written server, and Python receives four bytes of data that represents this integer, how can I parse it into an integer recognized by python? The standard Python module struct is used to solve this problem.
The struct module does not have much content and is not too difficult. The following describes the most common methods:
Struct. Pack
Struct. pack is used to convert Python values to strings Based on format characters (because Python does not have the byte type, you can understand the strings here as byte streams or byte arrays ). The function prototype is struct. Pack (FMT, V1, V2,...). The FMT parameter is a format string. The information about the format string is described below. V1, V2,... indicates the python value to be converted. The following example converts two integers to a string (byte stream ):
Import struct </P> <p> A = 20 <br/> B = 400 </P> <p> STR = struct. pack ("II", a, B) # although the converted STR is of the string type, it is equivalent to the byte stream (byte array) in other languages ), can be transmitted over the network <br/> Print 'length: ', Len (STR) <br/> Print STR <br/> Print repr (STR) </P> <p> # ---- result <br/> # length: 8 <br/> # ---- garbled characters <br/> # '/x14/x00/x00/x00/x90/x01/x00/x00'
The format character "I" indicates the conversion to int, and 'II' indicates that there are two int variables. The length of the converted result is 8 bytes (the int type occupies 4 bytes and the two int values are 8 bytes). The output result is garbled because the result is binary data, so it is garbled. You can use the python built-in function repr to obtain recognizable strings. The hexadecimal 0x00000014 and 0x00001009 indicate 20 and 400 respectively.
Struct. Unpack
Struct. Unpack is the opposite of struct. Pack. It is used to replace byte Transfer with Python data type. Its function is prototype: struct. Unpack (FMT, string). This function returnsTuples. The following is a simple example:STR = struct. pack ("II", 20,400) <br/> a1, a2 = struct. unpack ("II", STR) <br/> Print 'a1: ', A1 <br/> Print 'a2 :', a2 </P> <p> # ---- result: <br/> # A1: 20 <br/> # A2: 400 <br/>
Struct. calcsize
Struct. calcsize is used to calculate the length of the result corresponding to the format string, for example, struct. calcsize ('II'). 8 is returned. Because the length of two int types is 8 bytes.
Struct. pack_into, struct. unpack_from
These two functions are described in the python manual, but no examples are provided for use. In fact, they are rarely used in practical applications. After Google for a long time, I found an example and posted it for sharing:
Import struct <br/> from ctypes import create_string_buffer </P> <p> Buf = create_string_buffer (12) <br/> Print repr (BUF. raw) </P> <p> struct. pack_into ("III", Buf, 0, 1, 2,-1) <br/> Print repr (BUF. raw) </P> <p> Print struct. unpack_from ('II', Buf, 0) </P> <p> # ---- result <br/> #'/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00/x00 '<br/> #'/x01/x00/x00/x00/x02/x00/x00/x00/xFF '<br/> # (1, 2,-1)
About format strings
In the python manual, the format operators of common types in C language and Python type are given:
| Format character |
C Language |
Python type |
Note |
| X |
Pad byte |
No value |
|
| C |
Char |
String of length 1 |
|
| B |
Signed Char |
Integer |
|
| B |
Unsigned char |
Integer |
|
| ? |
_ Bool |
Bool |
|
| H |
Short |
Integer |
|
| H |
Unsigned short |
Integer |
|
| I |
Int |
Integer |
|
| I |
Unsigned int |
Integer or long |
|
| L |
Long |
Integer |
|
| L |
Unsigned long |
Long |
|
| Q |
Long long |
Long |
|
| Q |
Unsigned long |
Long |
|
| F |
Float |
Float |
|
| D |
Double |
Float |
|
| S |
Char [] |
String |
|
| P |
Char [] |
String |
|
| P |
Void * |
Long |
For details, refer to the python manual struct module.