Python struct.pack explained
Python struct.pack Detailed Explanation
1. Introduction
struct.pack
is a function in Python used to pack data into a specified format. The struct module provides powerful string formatting techniques for converting data to and from C structures. This article will detail how to use struct.pack
and provide some example code.
2. Instruction Format
The basic format of the struct.pack
function is as follows:
struct.pack(format, v1, v2, ...)
Where format
is a format string that specifies how data is packed; v1, v2, ...
are the data to be packed.
3. Format String
In the format
parameter of the struct.pack
function, you can use a format string to specify the type and order of the data to be packed.
A format string is a string consisting of format characters, each of which specifies a data type.
Common format characters are as follows:
x
: Represents a byte placeholderc
: Packs a character into a byteb
: Packs an integer into a byteB
: Packs an integer into an unsigned byteh
: Packs an integer into a short integerH
: Packs an integer into an unsigned short integeri
: Packs an integer into a Packs integers into an integerI
: Packs integers into an unsigned integerl
: Packs integers into a long integerL
: Packs integers into an unsigned long integerf
: Packs floating-point numbers into a single-precision floating-point numberd
: Packs floating-point numbers into a double-precision floating-point numbers
: Converts a string to a string (default length)p
:s
is similar, but a null character is appended to the string.q
: Packs an integer into a long-long integer.Q
: Packs an integer into an unsigned long-long integer.
In addition, special characters can be used in the format string to control the packing method:
@
: Local byte order=
: Standard byte order<
: Little-endian byte order>
: Big-endian byte order!
: Network byte order
4. Example Code
Below is some example code using the struct.pack
function.
4.1 Packed Integers
import struct
i = 42
packed_data = struct.pack('i', i)
print(packed_data)
Running Result:
b'*x00x00x00'
4.2 Packed Floating-Point Numbers
import struct
f = 3.14
packed_data = struct.pack('f', f)
print(packed_data)
Running Result:
b'*x00x00x00'
line-numbers”>b’xdbx0fI@’
4.3 Packing Strings
import struct
s = 'Hello, world!'
packed_data = struct.pack('s', s.encode())
print(packed_data)
Running Result:
b'Hello, world!'
4.4 Packing Multiple Data
import struct
i = 42
f = 3.14
s = 'Hello, world!'
packed_data = struct.pack('if10s', i, f, s.encode())
print(packed_data)
Running result:
b'*x00x00x00x9ax99x99?Hello, worl'
In this example, the format string is 'if10s'
, which means to pack an integer first, then a floating-point number, and finally a string of length 10.
5. Conclusion
This article introduced the use of the struct.pack
function. The format string allows you to flexibly specify the type and order of the data to be packed. In actual development, using the struct.pack
function can conveniently pack and unpack data, making it particularly suitable for data exchange with other languages.