search
HomeBackend DevelopmentPython TutorialDetailed introduction to the struct.pack() function and struct.unpack() function in python

The struct in python is mainly used to process C structure data. When reading, it is first converted to Python's string type, and then converted to Python's structured type, such as tuple. Generally, the input channels come from files or network binary streams.

  1.struct.pack() and struct.unpack()

During the conversion process, a format strings is mainly used. Use To specify the conversion method and format.

Let’s talk about the main methods:

1.1 struct.pack(fmt,v1,v2,...)

Put v1, v2 and other parameters The value is wrapped in one layer, and the wrapping method is specified by fmt. The wrapped parameters must strictly conform to fmt. Finally returns a wrapped string .

 1.2 struct.unpack(fmt,string)

 As the name suggests, unpack. For example, pack is packaged, and then unpacked can be used to unpack. Returns a tuple (tuple) obtained by unpacking data (string), even if there is only one data, it will be unpacked into a tuple. Among them, len(string) must be equal to calcsize(fmt), which involves a calcsize function. struct.calcsize(fmt): This is used to calculate the size of the structure described in the fmt format.

The format string consists of one or more format characters. For the description of these format characters, refer to the Python manual as follows

##xpad byte no valueccharstring of length 1bsignedcharintegerBunsignedchar integer?_Boolbool(1)hshortintegerHunsignedshortintegeriintintegerIunsignedintinteger or longllongintegerLunsignedlonglongqlonglonglong(2)Qunsignedlonglonglong(2)ffloatfloatddoublefloatschar[]stringpchar[]stringPvoid*long
Format c Type Python #Note

 2. Code example

import struct 

# native byteorder 
buffer = struct.pack("ihb", 1, 2, 3) 
print repr(buffer) 
print struct.unpack("ihb", buffer) 

# data from a sequence, network byteorder 
data = [1, 2, 3] 
buffer = struct.pack("!ihb", *data)
print repr(buffer) 
print struct.unpack("!ihb", buffer)

 

 

Output:

'\x01\x00\x00\x00\x02\x00\x03'
(1, 2, 3)
'\x00\x00\x00\x01\x00\x02\x03'
(1, 2, 3)

View Code

First, pack the parameters 1, 2, and 3. Before packing, 1, 2, and 3 obviously belong to the integer in the python data type. After packing, they become a C-structured binary string and are converted into the python string type for display. '\x01\x00\x00\x00\x02\x00\x03'. Since this machine is little endian ('little-endian', please refer to here for the difference between big endian and little endian, so the high bits are placed in the low address segment. i represents the int type in the C struct, so this machine occupies 4 bits, 1 Expressed as 01000000; h represents the short type in the C struct, occupying 2 bits, so it is expressed as 0200; similarly, b represents the signed char type in the C struct, occupying 1 bit, so it is expressed as 03.

Others. The conversion of the structure is also similar. For some special ones, you can refer to the Manual of the official document.

At the beginning of the Format string, there is an optional character to determine the big endian and little endian. The list is as follows:

##@=##>big-endianstandard!network (= big-endian)standard

If not appended, the default is @. Even if the native character order (big endian or little endian) is used, the size of the C structure and the alignment in memory are consistent with the native machine (native). For example, The integer of some machines is 2 bits, while some machines have four bits; some machine memories have four bits aligned, and some have n bits aligned (n is unknown, and I don't know how many).

There is also a standard option, which is described as: If standard is used, there will be no memory alignment for any type.

For example, in the second half of the applet just now, the first bit in the format string used is! , which is the standard alignment of big endian mode, so the output is '\x00\x00\x00\x01\x00\x02\x03', in which the high bit itself is placed in the high address bit of the memory.

For more detailed introductions to the struct.pack() function and struct.unpack() function in Python, please pay attention to the PHP Chinese website!

 

native native
native standard
little-endian standard
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
What data types can be stored in a Python array?What data types can be stored in a Python array?Apr 27, 2025 am 12:11 AM

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

What happens if you try to store a value of the wrong data type in a Python array?What happens if you try to store a value of the wrong data type in a Python array?Apr 27, 2025 am 12:10 AM

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

Which is part of the Python standard library: lists or arrays?Which is part of the Python standard library: lists or arrays?Apr 27, 2025 am 12:03 AM

Pythonlistsarepartofthestandardlibrary,whilearraysarenot.Listsarebuilt-in,versatile,andusedforstoringcollections,whereasarraysareprovidedbythearraymoduleandlesscommonlyusedduetolimitedfunctionality.

What should you check if the script executes with the wrong Python version?What should you check if the script executes with the wrong Python version?Apr 27, 2025 am 12:01 AM

ThescriptisrunningwiththewrongPythonversionduetoincorrectdefaultinterpretersettings.Tofixthis:1)CheckthedefaultPythonversionusingpython--versionorpython3--version.2)Usevirtualenvironmentsbycreatingonewithpython3.9-mvenvmyenv,activatingit,andverifying

What are some common operations that can be performed on Python arrays?What are some common operations that can be performed on Python arrays?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

In what types of applications are NumPy arrays commonly used?In what types of applications are NumPy arrays commonly used?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

When would you choose to use an array over a list in Python?When would you choose to use an array over a list in Python?Apr 26, 2025 am 12:12 AM

Useanarray.arrayoveralistinPythonwhendealingwithhomogeneousdata,performance-criticalcode,orinterfacingwithCcode.1)HomogeneousData:Arrayssavememorywithtypedelements.2)Performance-CriticalCode:Arraysofferbetterperformancefornumericaloperations.3)Interf

Are all list operations supported by arrays, and vice versa? Why or why not?Are all list operations supported by arrays, and vice versa? Why or why not?Apr 26, 2025 am 12:05 AM

No,notalllistoperationsaresupportedbyarrays,andviceversa.1)Arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,whichimpactsperformance.2)Listsdonotguaranteeconstanttimecomplexityfordirectaccesslikearraysdo.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools