Python is a very concise language, and for the representation of data types, unlike other languages, many types are predefined (for example, in C #, there are 8 types of optical integers), it defines only six basic types: strings, integers, floating-point numbers, tuples, lists, dictionaries. With these six data types, we can do most of the work. But when Python needs to interact with other platforms over the network, it must take into account the conversion of these data types to the types of other platforms or languages. For example: the C + + write client sends an INT (4 bytes) variable data to a Python-written server, and Python receives 4 bytes of data representing the integer, how to parse it into a python-recognized integer? Python's standard module struct is used to solve this problem.
The content of the struct module is not too much, it is not too difficult, the following is the most commonly used methods are described:
Struct.pack
Struct.pack is used to convert Python values from a format character to a string (because there is no byte type in Python, you can interpret the string here as a byte stream, or an array of bytes). Its function prototype is: Struct.pack (FMT, V1, v2, ...), parameter FMT is a format string. V1, v2, ... Represents the Python value to convert. The following example converts two integers to a string (byte stream):
import struct A = 20b = all str = struct.pack ("II", A, b) #转换后的str虽然是字符串类型, but equivalent to a byte stream in another language (an array of bytes) that can be transmitted over the network print ' length: ', le N (str) print strprint repr (str) #----result#length:8# ----Here is the garbled # '/x14/x00/x00/x00/x90/x01/x00/x00 '
The format character "I" represents the conversion to int, ' II ' means there are two int variables. The result of the conversion is 8 bytes long (the int type occupies 4 bytes, two int is 8 bytes), you can see the output is garbled, because the result is binary data, so it is displayed as garbled. You can use Python's built-in function repr to get a recognizable string, where 16 binary 0x00000014, 0x00001009 20 and 400, respectively.
Struct.unpack
Struct.unpack does work just as opposed to struct.pack, which is used to convert byte streams into Python data types. The function prototype is: Struct.unpack (FMT, String), which returns a tuple. The following is a simple example:
str = struct.pack ("II", "A1"), a2 = Struct.unpack ("II", str) print ' A1: ', A1print ' A2: ', A2 #----Result: #a1:20#a2:4 00struct.calcsize
The struct.calcsize is used to calculate the length of the result of the format string, such as: Struct.calcsize (' II '), which returns 8. Because two int types occupy a length of 8 bytes.
Struct.pack_into, Struct.unpack_from
These two functions are described in the Python manual, but there are no examples of how to use them. In fact, they do not use much in practical applications. Google for a long time, only to find an example, paste out to share a bit:
Import structfrom ctypes import Create_string_buffer buf = Create_string_buffer () print repr (Buf.raw) Struct.pack_into ("III", BUF, 0, 1, 2,-1) Print repr (buf.raw) Print struct.unpack_from (' III ', BUF, 0) #----result# '/x00/x00/x00/x00/x00/x0 0/x00/x00/x00/x00/x00/x00 ' # '/x01/x00/x00/x00/x02/x00/x00/x00/xff/xff/xff/xff ' # (1, 2,-1)
About Format Strings
In the Python manual, a format character corresponding to the Python type in the C language is given:
Please refer to the Python manual struct module for details.