struct module in Python and its functions

In this tutorial, we are going to discuss the struct module in Python and its functions. This module is useful for the conversion between C struct and python values. We use a format string to specify the order and size of the values that are to be packed in our struct. Read until the end of the tutorial to understand the concept clearly.

struct module in Python

There are many functions defined in the struct module of python. Few of them have been discussed here.

struct.pack() and struct.unpack()

These two functions are respectively used to pack and unpack values in python struct object. The syntax for stuct.pack() is as follows:

struct.pack(format_string, value1, value2, ....);

This function takes parameters format_string and values to be packed in the struct. format_string specifies the format of python values. For example, ‘hhl’ for (short, short, long). Here h stands for short and l stands for long.

Other possible formats are:

‘iii’ = int, int, int
‘?qf’ = _Bool,  long long, float
‘hiB’ = short, int, unsigned char

We can use i,h,l,q,? etc in any order to format our string.

The return type for struct.pack() is a string containing the values provided in the function as parameters.

The syntax for the struct.unpack() is as follows

struct.unpack(format_string, struct_string);

This function returns the values that are packed in the struct_string. See the code for a better understanding.

import struct

struct_string = struct.pack('hhl', 1, 3 , 5)
print(struct_string)

values = struct.unpack('hhl', struct_string)
print(values)

Output:

b'\x01\x00\x03\x00\x05\x00\x00\x00'
(1, 3, 5)

Note that b in our struct_string stands for binary.

struct.calcsize()

We use this struct function to find the size of a format string. This can be useful when calling struct.pack_into and struct.unpack_from() function as they require the size of format string as input.

The syntax for the above function is given here:

struct.calcsize(format_string);

The function returns the size required by the format_string.

See the below code.

import struct

print(struct.calcsize('qf?'))
print(struct.calcsize('ll'))
print(struct.calcsize('?qf'))

Output:

13
8
20

As you can notice, changing the order in the format string affects the size of the struct.

struct.pack_into() and struct.unpack_from()

The syntax for struct.pack_into is as follows:

struct.pack_into(format_string, buf, offset, v1, v2, ...);

In the above syntax, format_string is to specify the data types and order of the values to be inserted in the struct. ‘buf’ is a writable buffer that starts at the offset. After that, we pass the values to be packed.

The syntax for struct.unpack_from() is as follows:

struct.unpack_from( format_string, buf[, offset =0]);

This function returns a tuple with packed values.

See the below code to understand the working of these functions.

import struct
import ctypes

size = struct.calcsize('iii')

buf = ctypes.create_string_buffer(size)

struct.pack_into('iii', buf, 0, 5, 5, 5) 

print(struct.unpack_from('iii', buf, 0))

Output:

(5, 5, 5)

NOTE: This module also defines an exception struct.error. This exception is raised when we pass a wrong argument in the above functions.

Thank you.

Also read: Tuple Constructor in Python Language

Leave a Reply

Your email address will not be published. Required fields are marked *