Tutorials  /  Python

Python struct pack, unpack

Ccentron Redaktion · February 2024 ·3 min read ·Python, Tutorial

Python struct module is capable of performing the conversions between the Python values and C structs, which are represented as Python Strings.

Python struct

  • Python struct module can be used in handling binary data stored in files, database or from network connections etc.
  • It uses format Strings as compact descriptions of the layout of the C structs and the intended conversion to/from Python values.
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

Functions

There are five important functions in struct module - pack(), unpack(), calcsize(), pack_into() and unpack_from(). In all these functions, we have to provide the format of the data to be converted into binary. Some of the popular format characters are:

  • ?: boolean
  • h: short
  • l: long
  • i: int
  • f: float
  • q: long long int

You can get the complete list of format characters here. Let’s start looking into struct module functions one by one.

Python struct.pack()

This function packs a list of values into a String representation of the specified type. The arguments must match the values required by the format exactly. Let’s quickly look at struct pack() example:

Python
import struct

var = struct.pack('hhl', 5, 10, 15)
print(var)
 
var = struct.pack('iii', 10, 20, 30)
print(var)

Python struct.unpack()

This function unpacks the packed value into its original representation with the specified format. This function always returns a tuple, even if there is only one element. Let’s quickly look at struct unpack() function example:

Python
import struct
var = struct.pack('hhl', 5, 10, 15)
print(var)
print(struct.unpack('hhl', var))

Python struct calcsize()

This function calculates and returns the size of the String representation of struct with a given format. Size is calculated in terms of bytes. Let’s quickly look at an example code snippet:

Python
import struct
 
var = struct.pack('hhl', 5, 10, 15)
print(var)
print("Size of String representation is {}.".format(struct.calcsize('hhl')))

Python struct pack_into(), unpack_from()

These functions allow us to pack the values into string buffer and unpack from a string buffer. These functions are introduced in version 2.5.

Python
import struct
# ctypes is imported to create a string buffer
import ctypes

# As shown in previous example
size = struct.calcsize('hhl')
print(size)

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

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

Conclusion

That’s all for a short introduction of python struct module.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren