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:
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.
struct.unpack()
struct.unpack()
unpacks the byte stream into Python data type.
The function prototype:
This function return a tuple.
For example.
struct.calcsize()
Struct.calcsize()
is used to calculate the length of the result corresponding to the format string.
For example.