Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to convert to and from bytes in Python

📅 2014-Dec-29 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ pack, python, unpack ⬩ 📚 Archive

A typical scenario in systems programming is to read and write structures and data in a certain binary format. This could be using buffers in memory or binary files on disk. This is pretty straightforward to do in C or C++, all you need is a pointer to the start of the structure or data and the number of bytes to copy or write from that pointer. The struct module can be used to achieve the same in Python.

The pack method can be used to convert data to a byte string. For details of the parameters and formatting flags passed to this method, see here. This byte string can be then be passed on the network or written to a binary file. For example:

# Sample code to write data of certain format to binary file
# For details of parameters passed to pack method see:
# https://docs.python.org/2/library/struct.html

import struct

# Assume these are values of fields of a class/struct in C/C++
a = 1  # unsigned int
b = 2  # signed char
c = -1 # signed int

# To write a, b, c with padding to binary file
# This will write 12 bytes => (4, 4, 4)
f = open("foo1.bin", "wb")
s = struct.pack("Ibi", a, b, c)
f.write(s)
f.close()

# To write a, b, c with no padding to binary file
# This will write 9 bytes => (4, 1, 4)
f = open("foo2.bin", "wb")
s = struct.pack("=Ibi", a, b, c)
f.write(s)
f.close()

To read back binary data or data stored in bytes, use the unpack method. The usage of that is similar to above.

Tried with: Python 2.7.6 and Ubuntu 14.04


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧