Home >Backend Development >Python Tutorial >Detailed explanation of Python struct (byte stream, grouping and unpacking implementation) module

Detailed explanation of Python struct (byte stream, grouping and unpacking implementation) module

高洛峰
高洛峰Original
2016-10-19 11:12:052085browse

Everyone should be familiar with the English word struct, because there is struct in c/c++, where struct is called a structure. Struct is also used in Python, which fully demonstrates that this struct should have a deep origin with the struct in c/c++. Python uses the struct module to perform conversions between Python values ​​and C structures to form Python bytes objects. It uses a format string as a compact description of the underlying C structure and converts it into a Python value based on this format string.

We know that python only defines 6 data types, strings, integers, floating point numbers, lists, tuples, and dictionaries. But there are some byte-type variables in C language. How to implement them in python? This is quite important, especially if data is to be transmitted over the network.

Python provides a struct module to provide conversion. Here are some methods in this module.

pack(fmt, v1, v2, ...) ------ Convert the values ​​v1, v2, ... into a string according to the format described by the given fmt.

unpack(fmt, bytes) ------ Reversely parse the bytes according to the format described by the given fmt and return a tuple.

calcsize(fmt) ------ Returns the size of the structure according to the format described by the given fmt.

struct.pack():

struct.pack is used to convert the Python value into a string according to the format character (because there is no byte (Byte) type in Python, the string here can be understood as a byte stream , or byte array). The function prototype is: struct.pack(fmt, v1, v2, ...), the parameter fmt is the format string, and the relevant information about the format string is introduced below. v1, v2, ... represent the Python values ​​to be converted. The following example converts two integers into strings (byte streams):

>>> import struct

>>> a=20

>>> b=400

>>> str=struct.pack ('ii',a,b) #Convert to a byte stream, although it is still a string, it can be transmitted on the network

>>> print len(str)             #ii represents two ints

8                                                                      The length is 8 bytes, which is exactly the length of two int type data

>>> print str

                                                                                                                                                                                                   #Where hexadecimal 0x00000014, 0x00001009 respectively represent 20 and 400

>>>>

From this we can arbitrarily group packages, such as the following packaging example, which only introduces the pack

format = "!HH% ds" % len(data)

buffer = struct.pack(format,opcode,blocknumber,data)

We need to pack a data and add some headers. Based on the following format character information, we know that H is unsigned short is 2 bytes, and s is char type. So this buffer is 2 bytes of opcode, 2 bytes of blocknumber, and len long char.

Python module - struct (byte stream, grouping and unpacking implementation)

struct.unpack():

Let’s continue the above example and run:

>>> a1,a2=struct.unpack( 'ii',str)

>>> print 'a1',a1

a1 20

>>> print 'a2=',a2

a2= 400

You can see that "ii" starts with four characters Section is the demarcation point, and the 8-byte str is divided into two int type integers.

struct.calcsize(): Used to calculate the size of the output in a specific format, which is a few bytes, such as:

>>> struct.calcsize('HH4s')

8

>>> struct.calcsize ('ii')

8

>>>>

>>> format='!HH%ds' % len('hello python')

>>> struct.calcsize(format)

16

>>>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn