This module converts between Python values and C structures represented as Python byte objects. Format strings — it is a mechanism used to specify the expected layout when packing and unpacking data. The module structure is available in Python 3.x, not 2.x, so these codes will work in the Python3 interpreter.
Structural Functions
- struct.pack ()
Syntax: struct.pack (format, v1, v2, ...)
Return a string containing the values v1, v2, ... that are packed according to the given format (format strings — is the mechanism used to specify the expected layout when packing and unpacking data). The values followed by the format must only match the format, otherwise the struct.error is promoted.
import
struct
# Format: h short in type C
# Format: l long in type C
# Format “hhl” means “short short long”
var
=
struct.pack (
’hhl’
,
1
,
2
,
3
)
print
(var)
< / code>
# Format: I am int in type C
# Format "iii" means "int int int" .
var
=
struct.pack (
’iii’
,
1
,
2
,
3
)
print
(var)
Output:
b’x01x00x02x00x00x00x00x00x03x00x00x00x00x00x00x00’ b’x01x00x00x00x02x00x00x00x003’x00
- struct.unpack ()
Syntax: struct.unpack (fmt, string)
Return v1 values, v2, ..., which are unpacked according to the given format (1st argument). The values returned by this function are returned as tuples of size equal to the number of values passed through struct.pack () during packing.
import
struct
# & # 39 ;? & # 39; -" _BOOL, & # 39; h & # 39; -" short, & # 39; i & # 39; -" int and & # 39; l & # 39; -" long
var
=
struct.pack (
’? hil’
,
True
,
2
,
5
,
445
)
print
(var)
# struct.unpack () returns tuples
# Variables V1, V2, V3, .. are returned as elements of a tuple
tup
=
struct.unpack (
’? hil’
, var)
print
(tup)
# q -" long long int and f -" float
var
=
struct.pack (
’qf’
,
5
,
2.3
)
print
(var)
tup
=
struct.unpack (
’qf’
, var)
print
(tup)
Output:
b’x01x00x02x00x05x00x00x00xbdx01x00x00x00x00x00x00’ (True, 2, 5, 445) b’x05x00x00x00x00x00x00x0033x13 @ ’(5 / pre>
Note: "b" in the output means binary.
- struct.calcsize ()
Syntax: struct. calcsize (fmt) fmt: form at
Return the size of the structure (and therefore the line) to match the given format. calcsize () is an important function and is required for functions like struct.pack_into () and struct.unpack_from (), which also require offset and buffer values.
import
struct
var
=
struct.pack (
’? hil’
,
True
,
2
,
5
,
445
)
print
(var)
# Returns the size of the structure
print
(struct.calcsize (
’? hil’
))
print
(struct.calcsize (
’qf’
))
Output:
b’x01x00x02x00x05x00x00x00xbdx01x00x00x00x00x00x00’ 16 12
import
struct
var
=
struct.pack (
’bi’
,
56
,
0x12131415
)
print
(var)
print
(struct.calcsize (
’ bi’
))
var
=
struct. pack (
’ib’
,
0x12131415
,
56
)
print
(var)
print
( struct.calcsize (
’ib’
))
Output:
b’8x00x00x00x15x14x13x12’ 8 b’x15x14x13x128’ 5
Note. The order of the format characters can affect the size.
- The struct.error exception
The struct.error exception describes what is wrong with passing arguments when an invalid argument is passed struct.error.from
struct
import
error
print
(error)
Note. This piece of code is useless for anything other than exception handling and is used to demonstrate that a "error" when interpreted shows a class.
- struct.pack_into ()
Syntax: struct.pack_into (fmt, buffer, offset, v1, v2, ...) fmt: data type format buffer: writable buffer which starts at offset (optional) v1, v2 .. : values
- struct.unpack_from ()
Syntax: struct.unpack_from (fmt, buffer [, offset = 0]) fmt: data type format buffer: writable buffer which starts at offset (optional )
Returns a tuple similar to struct.unpack ()
import
struct
# ctypes in import to create a string buffer
import
ctypes
# Format SIZE is calculated using calcsize ()
siz
=
struct.calcsize (
’hhl’
)
print
(siz)
# Buffer & # 39; buff & # 39; created
buff
=
ctypes.create_string_buffer ( siz)
# struct.pack () returns packed data code>
# struct.unpack () returns unpacked data
x
=
struct.pack (
’hhl’
,
2
,
2
,
3
)
print
(x)
print
(struct.unpack (
’hhl’
, x))
# struct.pack_into () packs data into a buff, returns no value
# struct.unpack_from () unpacks data from buff, returns a tuple of values
struct.pack_into (
’hhl’
, buff,
0
,
2
,
2
,
3
)
print
(struct.unpack_from (
’ hhl’
, buff,
0
))
Output:
16 b’x02x00x02x00x00x00x00x00x03x00x00x00x00x00x00x00’ ( 2, 2, 3) (2, 2, 3)
Link https://docs .python.org / 2 / library / struct.html
This article courtesy of Piyush Doorvar . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.
Shop
Latest questions
Wiki
- struct.calcsize ()