


Detailed 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
Format | c Type | Python | #Note |
---|---|---|---|
pad byte | no value | ||
char | string of length 1 | ||
signedchar | integer | ||
unsignedchar | integer | ||
_Bool | bool | (1) | |
short | integer | ||
unsignedshort | integer | ||
int | integer | ||
unsignedint | integer or long | ||
long | integer | ||
unsignedlong | long | ||
longlong | long | (2) | |
unsignedlonglong | long | (2) | |
float | float | ||
double | float | ||
char[] | string | ||
char[] | string | ||
void* | long |
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:native | native | |
native | standard | |
little-endian | standard | ##> |
standard | ! | |
standard |

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Fastapi ...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.
