Tutorial

Python struct pack, unpack

Published on August 3, 2022
author

Shubham

Python struct pack, unpack

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.

Python Struct 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:

import struct

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

When we run this script, we get the following representation: python struct pack Note that ‘b’ in the Output stands for binary.

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:

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

When we run this script, we get back our original representation: python struct unpack Clearly, we must tell the Python interpreter the format we need to unpack the values into.

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:

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

When we run this script, we get the following representation: python struct calcsize

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.

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))

When we run this script, we get the following representation: Python Struct pack_into and unpack_from string buffer That’s all for a short introduction of python struct module.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Shubham

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
June 9, 2021

I agree, the way the struct var prints is ridiculous. How did it get past the checking process?

- Pete

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 2, 2021

    is it possible to switch off this feature and get it ‘uneasy’ ?

    - wally

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      April 8, 2019

      why 10 and 13 return b’\n\x00\r\x00’

      - Richard Ng

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Featured on Community

        Get our biweekly newsletter

        Sign up for Infrastructure as a Newsletter.

        Hollie's Hub for Good

        Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

        Become a contributor

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        Welcome to the developer cloud

        DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

        Learn more