ustruct
– Pack and unpack binary data¶
This module provides functions to convert between Python values and C-like data structs.
- calcsize(format: str) int ¶
Gets the data size corresponding to a format string
- Parameters:
format (str) – Data format string.
- Returns:
The number of bytes needed to represent this format.
- pack(format, value1, value2, ...)¶
Packs the values using the given format.
- Parameters:
format (str) – Data format string.
- Returns:
The data encoded as bytes.
- pack_into(format, buffer, offset, value1, value2, ...)¶
Encode the values using the given format and write them to a given buffer.
- unpack(format, data) Tuple ¶
Decodes the binary data using the given format.
- unpack_from(format, data, offset) Tuple ¶
Decodes binary data from a buffer using the given format.
The following byte orders are supported:
Character |
Byte order |
Size |
Alignment |
---|---|---|---|
@ |
native |
native |
native |
< |
little-endian |
standard |
none |
> |
big-endian |
standard |
none |
! |
network (= big-endian) |
standard |
none |
The following data types are supported:
Format |
C Type |
Python type |
Standard size |
---|---|---|---|
b |
signed char |
integer |
1 |
B |
unsigned char |
integer |
1 |
h |
short |
integer |
2 |
H |
unsigned short |
integer |
2 |
i |
int |
integer |
4 |
I |
unsigned int |
integer |
4 |
l |
long |
integer (1) |
4 |
L |
unsigned long |
integer (1) |
4 |
q |
long long |
integer (1) |
8 |
Q |
unsigned long long |
integer (1) |
8 |
f |
float |
float |
4 |
d |
double |
float |
8 |
s |
char[] |
bytes |
|
P |
void * |
integer |
(1) Supports values up to +/-1073741823