Struct Module In Python

What is a struct module?

Struct module performs conversion between python value and C structs represented as python bytes objects. which is used in handling binary data stored in files or from network connections among other resources. It uses format strings used to specify the expected layout when packing and unpacking data.

Note: This module is available in python 3. x not in earlier versions. Thus this code will run on the python3 interpreter.

1 Packing data using struct.pack()

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

Here values specified after format will be packed according to the specified format. So we have to specify value as per format otherwise it will return struct.error

# packing values
#format: h is short, l is long type, and i for int type.  
from struct import *
packing = pack('hhl',2,3,5)
print(packing)

OR

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

Output: 
b'\x02\x00\x03\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00'

# same values but with iii formats.

from struct import *
packing = pack('iii', 2, 3, 5)
print(packing)

Output: 
b'\x02\x00\x00\x00\x03\x00\x00\x00\x05\x00\x00\x00' 

2. Unpacking values :

Syntax : struct.unpack(format, string)

Values returned by this function are returned as tuples of size which is equal to the values we passed during packing.

#Unpacking 
# here q is long long int and f is float values. 

import struct
packing= struct.pack('qf', 8, 8.3)
print(packing)

Output : 
b'\x08\x00\x00\x00\x00\x00\x00\x00\xcd\xcc\x04A'

unpacking = struct.unpack('qf', packing)
print(unpacking)

Output: 
(8, 8.300000190734863)

3. struct.calcsize(fmt):

This will return the size of the struct for the given format. it is used when the struct function requires offset values and buffer as well.

#struct.calcsize()

import struct
# Returns the size of the structure
print(struct.calcsize('hll'))
print(struct.calcsize('iii'))


Output: 
24
12

4. Struct.pack_into()

Syntax: struct.pack_into(format, buffer, offset, v1, v2, ..)

Pack the values according to the format string format. And write packed bytes into the writable buffer. here offset is a required argument.

Note:  keep in mind that this does not return any value but it is just packed data into buff.

5. struct.unpack_from()

Syntax: struct.unpack_from(format, buffer, offset=0)

This will return a tuple as a result even if it is only one item and buffer size must be at least the size required by format as reflected by calcsize().

Example :

#struct.pack_into() and struct.pack_from()

import struct
# ctypes in imported to create string buffer
import ctypes

#format is calculated using calcsize()
size = struct.calcsize('hhl')
print(size)

# Buffer 'buff' is created
buff = ctypes.create_string_buffer(size)

# struct.pack() returns packed data
# struct.unpack() returns unpacked data
x = struct.pack('hhl', 2, 5, 3)
print(x)
print(struct.unpack('hhl', x))

# struct.pack_into() packs data into buff, doesn't return any value
# struct.unpack_from() unpacks data from buff, returns a tuple of values
print(struct.pack_into('hhl', buff, 0, 2, 5, 3))
print(struct.unpack_from('hhl', buff, 0))


Output : 
16
b'\x02\x00\x05\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00'
(2, 5, 3)
None
(2, 5, 3)

That is pretty much. For more have a look at  Reference.

Thanks for the read.

Submit a Comment

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

Subscribe

Select Categories