Python Binary Data Services


Provisions of the struct module in the Python library are useful in performing conversions between C type structs and Python bytes objects. This can be achieved by module level functions as well as Struct class and its methods as defined in the struct module.

The conversion functions use a format string. The byte order, size, and alignment used in the format string is determined by formatting character as per the following table

Character
Byte order
Size
Alignment
@
native
native
native
=
native
standard
none
<
little-endian
standard
none
>
big-endian
standard
none
!
network (= big-endian)
standard
none

Following table shows format characters used to denote C type variables and corresponding Python types.

Format
C Type
Python type
x
pad byte
no value
c
char
bytes of length 1
b/B
signed/unsigned char
integer
?
_Bool
bool
h/H
short/unsigned short
integer
i/I
int/unsigned int
integer
l/L
long/unsigned long
integer
f
float
float
d
double
float
s
char[]
bytes
p
char[]
bytes
P
void *
integer

Following functions are defined in the struct module

pack()

This function returns a bytes object containing the values packed according to the format string format. The formatting characters must match the values required by the format.

unpack()

This function unpacks from the buffer according to the format string format. The result is a tuple even if it contains exactly one item.

Following code demonstrates the use of these functions.

import struct
student=(1, b'Rahul', 65.75)
packed=struct.pack('I 5s f', *student)
print ('packed data:',packed)

unpacked=struct.unpack('I 5s f', packed)
print ('unpacked data:',unpacked)

Output

packed data: b'\x01\x00\x00\x00Rahul\x00\x00\x00\x00\x80\x83B'
unpacked data: (1, b'Rahul', 65.75)

Packing/unpacking can also be achieved by using methods in the Struct class. Creating a Struct object once and calling its methods is more efficient than calling the struct functions with the same format since the format string only needs to be compiled once.

Struct(format)

This constructor returns a new Struct object which writes and reads binary data according to the format string format.

pack()

This method is identical to the pack() function, using the compiled format.

unpack()

This method is identical to the unpack() function, using the compiled format.

Following example shows how Struct class is used to pack and unpack Python data.

s = struct.Struct('I 5s f')
packed=s.pack(*student)
print (packed)
unpacked = s.unpack(packed)
print (unpacked)

The packed data can be directly parsed to a named tuple object.

from collections import namedtuple
newstudent = namedtuple('newstudent','No Name Marks')
s1 = newstudent._make(struct.unpack('I 5s f', packed))
print (s1)

Output

newstudent(No=1, Name=b'Rahul', Marks=65.75)

Updated on: 30-Jul-2019

258 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements