ustruct
– Pack and decompressing raw data types¶
This module implements the corresponding CPython a subset of modules, as follows. Refers to CPython document for details: struct
Format string¶
Format strings are a mechanism to specify the expected layout when pack and unpack data. In addition, there are special characters for controlling byte order, size, and alignment.
Byte order, size, and alignment¶
By default, type C is represented in the native format and byte order of the machine,And if necessary, correct alignment by skipping padding bytes (according to the rules used by the C compiler). Or, depending on the table below, the first character of the format string can be used to indicate the byte order, size, and alignment of the packed data:
Character | Byte order | Size | Alignment |
---|---|---|---|
@ |
native | native | native |
= |
native | standard | none |
< |
little-endian | standard | none |
> |
big-endian | standard | none |
! |
network (= big-endian) | standard | none |
Format character¶
Format characters have the following implication; Depending on the type, the conversion between C and Python values should be obvious. “Standard size” column refers to the size (in bytes) of the packed value when the standard size is used; That is, when one of the format strings starts’<’,’>’,’!’ or ‘=’. When using native size, the size of the packaging value depends on the platform.
Format | C Type | Python type | Standard size |
---|---|---|---|
x |
pad byte | no value | |
c |
char |
bytes 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 |
n |
ssize_t |
integer | |
N |
size_t |
integer | |
e |
(7) | float | 2 |
f |
float |
float | 4 |
d |
double |
float | 8 |
s |
char[] |
bytes | |
p |
char[] |
bytes | |
P |
void * |
integer |
Function¶
-
ustruct.
calcsize
(fmt)¶ Return to the given fmt number of bytes.
fmt
- Format character type, see format character table above
>>> struct.calcsize("i") 4 >>> struct.calcsize("B") 1
-
ustruct.
pack
(fmt, v1, v2, ...)¶ According to the format string FMT, pack v1, v2, … value. The return value is a byte object that decodes the value.
>>> struct.pack("ii", 3, 2) b'\x03\x00\x00\x00\x02\x00\x00\x00'
-
ustruct.
pack_into
(fmt, buffer, offset, v1, v2, ...)¶ According to the format string FMT. Take v1, v2, … Values are packed into a buffer starting with offset. Count from the end of the buffer, offset may be negative.
-
ustruct.
unpack
(fmt, data)¶ Decompress the data according to the format string fmt. The return value is a tuple of decompressed values.
>>> buf = struct.pack("bb", 1, 2) >>> print(buf) b'\x01\x02' >>> print(struct.unpack("bb", buf)) (1, 2)
-
ustruct.
unpack_from
(fmt, data, offset=0)¶ Unpack data starting at
offset
according to format stringfmt
. The offset to count from the end of the buffer may be negative. The return value is a tuple of the decompressed value.>>> buf = struct.pack("bb", 1, 2) >>> print(struct.unpack("bb", buf)) (1, 2) >>> print(struct.unpack_from("b", buf, 1)) (2,)