David Dong

David Dong

Java/C/C#/Python

Java/C/C#/Python

POST

Python struct.pack()

Python uses struct.pack() to pack python data type into binary data (Byte Stream), which means convert a value to a string based on the format.

Because there is no byte type in Python, where the string is equivalent to a byte stream, or byte array.

struct.pack()

The function prototype:

struck.pack(format, data1, data2,...)

format: define the converted format. data1,data2,…: the data needs to be packed.

The format includes

Format C Type Python Type Standard Size
x pad byte    
c char string of length 1 1
b signed char integer 1
B unsigned char integer 1
? _bool bool 1
h short integer 2
H unsigned short integer 2
i int integer 4
I unsigned int integer 4
l long integer 4
L unsigned long integer 4
q long long integer 8
Q unsigned long long integer 8
f float float 4
d double float 8
s char[] string  
p char[]string    
P void * integer  

To exchange data with the structure in C, it is also necessary to consider that some C or C + + compilers use byte alignment, usually a 32-bit system with 4 bytes as the unit. Therefore, a struct can be converted according to the byte order of the local machine. The alignment can be changed by the first character in the format. The definition is as follows:

Format Byte Order Size alignment
@ native byte native native
= native standard none
< little-endian char standard none
> big-endian char standard none
! network (= big-endian) standard none

Use it in the first place of format.

for example.

import struct

a = 11
b = 12

print(len(struct.pack("ii",a,b)))
8
print(struct.pack("ii",a,b))
b'\x0b\x00\x00\x00\x0c\x00\x00\x00'

struct.unpack()

struct.unpack() unpacks the byte stream into Python data type.
The function prototype:

struct.unpack(fmt, string)

This function return a tuple.

For example.

a = 11
b = 12
packdata = struct.pack("ii",a,b)
c,d = struct.unpack("1i1i",packdata)
print((c,d))
(11,12)

struct.calcsize()

Struct.calcsize() is used to calculate the length of the result corresponding to the format string.

For example.

print(struct.calcsize("c"))
1
print(struct.calcsize("H"))
2
print(struct.calcsize("L"))
4
print(struct.calcsize("Q"))
8

Python

You may also like

further reading