


Detailed description of Python struct byte stream, grouping and unpacking implementation module
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',a1a1 20>>> print 'a2=',a2a2= 400You 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!

The basic syntax for Python list slicing is list[start:stop:step]. 1.start is the first element index included, 2.stop is the first element index excluded, and 3.step determines the step size between elements. Slices are not only used to extract data, but also to modify and invert lists.

Listsoutperformarraysin:1)dynamicsizingandfrequentinsertions/deletions,2)storingheterogeneousdata,and3)memoryefficiencyforsparsedata,butmayhaveslightperformancecostsincertainoperations.

ToconvertaPythonarraytoalist,usethelist()constructororageneratorexpression.1)Importthearraymoduleandcreateanarray.2)Uselist(arr)or[xforxinarr]toconvertittoalist,consideringperformanceandmemoryefficiencyforlargedatasets.

ChoosearraysoverlistsinPythonforbetterperformanceandmemoryefficiencyinspecificscenarios.1)Largenumericaldatasets:Arraysreducememoryusage.2)Performance-criticaloperations:Arraysofferspeedboostsfortaskslikeappendingorsearching.3)Typesafety:Arraysenforc

In Python, you can use for loops, enumerate and list comprehensions to traverse lists; in Java, you can use traditional for loops and enhanced for loops to traverse arrays. 1. Python list traversal methods include: for loop, enumerate and list comprehension. 2. Java array traversal methods include: traditional for loop and enhanced for loop.

The article discusses Python's new "match" statement introduced in version 3.10, which serves as an equivalent to switch statements in other languages. It enhances code readability and offers performance benefits over traditional if-elif-el

Exception Groups in Python 3.11 allow handling multiple exceptions simultaneously, improving error management in concurrent scenarios and complex operations.

Function annotations in Python add metadata to functions for type checking, documentation, and IDE support. They enhance code readability, maintenance, and are crucial in API development, data science, and library creation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
