Detailed description of Python struct byte stream, grouping and unpacking implementation module

高洛峰
Release: 2017-03-17 16:21:09
Original
2002 people have browsed it

Everyone should be familiar with the English word struct, because there is struct in c/c++, where struct is called 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 the format string as a compact description of the underlying C structure, and then 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 transferred 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, you can put it here The string is 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 byte stream, although it is still a string, but Can be transmitted on the network

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

8                       #You can see that the length is 8 characters section, which is exactly the length of two int type data

>>> print str

## str)

'\x14\x00\x00\x00\x90\x01\x00\x00' #Hexadecimal 0x00000014, 0x00001009 represent 20 and 400 respectively

>> ;>

From this we can arbitrarily group packages. For example, the following packaging example only introduces the pack

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

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

We want to pack a piece of data and add some headers. We based on From the format character information below, 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, packaging and unpacking implementation)

struct.unpack():

Let’s continue the above Example run:

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

>>> print 'a1',a1

a1 20

>>> print 'a2=',a2

a2= 400

You can see that "ii" is represented by four bytes For demarcation, the 8-byte str is divided into two integer types.

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

>>>

The above is the detailed content of Detailed description of Python struct byte stream, grouping and unpacking implementation module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!