Open In App

struct module in Python

Last Updated : 24 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This module performs conversions between Python values and C structs represented as Python bytes objects. Format strings are the mechanism used to specify the expected layout when packing and unpacking data. Module struct is available in Python 3.x and not on 2.x, thus these codes will run on the Python3 interpreter.

Python Struct Module

  • struct.pack()
  • struct.unpack()
  • struct.calcsize()
  • struct.pack_into()
  • struct.unpack_from()

struct.pack in Python

Syntax: struct.pack(format, v1, v2, …)

Return a string containing the values v1, v2, … , that are packed according to the given format (Format strings are the mechanism used to specify the expected layout when packing and unpacking data).The values followed by the format must be as per the format only, else struct.error is raised.

Struct Pack in Python Example

This code utilizes the struct module to pack values into binary data. The first struct.pack() call packs the values 1, 2, and 3 into binary data with specific format codes (‘h’ for short integer and ‘l’ for long integer), and the result is stored in the variable var. The second struct.pack() call attempts to pack three 4-byte signed integers with the format ‘iii’, which might raise an error due to possible range exceedance. The packed binary data or an exception message is printed accordingly.

Python3




import struct
var = struct.pack('hhl',1,2,3)
print(var)
var = struct.pack('iii',1,2,3)
print(var)


Output:

b'\x01\x00\x02\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00'

struct.unpack() in Python

Syntax: struct.unpack(fmt, string)

Return the values v1, v2, … , that are unpacked according to the given format(1st argument). Values returned by this function are returned as tuples of size that is equal to the number of values passed through struct.pack() during packing. 

Struct Unpack in Python Example

This code showcases the use of Python’s struct module to pack and unpack binary data with specified format codes. It demonstrates packing a combination of boolean, integer, long integer, and floating-point values into binary data and subsequently unpacking them. The format codes in the format strings dictate the data types and sizes, enabling precise data conversion between binary and Python data structures.

Python3




import struct
var = struct.pack('?hil', True, 2, 5, 445)
print(var)
tup = struct.unpack('?hil', var)
print(tup)
var = struct.pack('qf', 5, 2.3)
print(var)
tup = struct.unpack('qf', var)
print(tup)


Output:

b'\x01\x00\x02\x00\x05\x00\x00\x00\xbd\x01\x00\x00\x00\x00\x00\x00'
(True, 2, 5, 445)
b'\x05\x00\x00\x00\x00\x00\x00\x0033\x13@'
(5, 2.299999952316284)

Note: ‘b’ in the Output stands for binary.

struct.calcsize() in Python

Syntax: struct.calcsize(fmt)

  • fmt: format

Return the size of the struct (and hence of the string) corresponding to the given format. calcsize() is important function, and is required for function such as struct.pack_into() and struct.unpack_from(), which require offset value and buffer as well. 

Struct calcsize in Python Example

Example 1: This code uses Python’s struct module to pack boolean, integer, and long integer values into binary data using the format string '?hil'. It then calculates and prints the size (in bytes) of the packed binary data for this format. Additionally, it calculates and prints the size for the format string 'qf', which represents a long long integer followed by a floating-point number. This illustrates how the struct.calcsize() function can be used to determine the memory size of binary data representations based on format strings.

Python3




import struct
var = struct.pack('?hil', True, 2, 5, 445)
print(var)
print(struct.calcsize('?hil'))
print(struct.calcsize('qf'))


Output:

b'\x01\x00\x02\x00\x05\x00\x00\x00\xbd\x01\x00\x00\x00\x00\x00\x00'
16
12

Example 2: This code utilizes the struct' module to pack data into binary format using format strings. It demonstrates two scenarios, one where a standard integer and a signed integer are packed in the order ‘bi’, and another where the order is reversed to ‘ib’. The code calculates and prints the size (in bytes) of the resulting binary data for each format string, showcasing how the order of data types affects the binary representation and size.

Python3




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'8\x00\x00\x00\x15\x14\x13\x12'
8
b'\x15\x14\x13\x128'
5

Note: The ordering of format characters may have an impact on size.

Exception struct.error

Exception struct.error describes what is wrong at passing arguments, when a wrong argument is passed struct.error is raised. 

PYTHON




from struct import error
print(error)


Note: This is piece of code is not useful, anywhere other than exception handling, and is used to show that ‘error’ upon interpreted shows about the class.

struct.pack_into() in Python

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() in Python

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() 

Struct unpack_from in Python Example

This code uses Python’s struct' and ctypes' modules to work with binary data. It first calculates the size required for a format string, creates a string buffer of that size, and packs and unpacks data using the specified format string. The code demonstrates how to manipulate binary data in memory, showcasing packing and unpacking data into/from a buffer and utilizing the pack_into' and unpack_from' functions to access the binary content.

Python3




import struct
import ctypes
size = struct.calcsize('hhl')
print(siz)
buff = ctypes.create_string_buffer(siz)
x = struct.pack('hhl', 2, 2, 3)
print(x)
print(struct.unpack('hhl', x))
struct.pack_into('hhl', buff, 0, 2, 2, 3)
print(struct.unpack_from('hhl', buff, 0))


Output:

16
b'\x02\x00\x02\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
(2, 2, 3)
(2, 2, 3)

Reference https://docs.python.org/2/library/struct.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads