Skip to main content

This module performs conversions between Python values and C bit field structs represented as Python bytearrays.

Project description

buildstatus

About

This module is intended to have a similar interface as the python struct module, but working on bits instead of primitive data types (char, int, …).

Documentation: http://bitstruct.readthedocs.org/en/latest

Installation

pip install bitstruct

Example usage

See the test suite: https://github.com/eerimoq/bitstruct/blob/master/tests/test_bitstruct.py

A basic example of packing/unpacking four integers:

>>> from bitstruct import *
>>> pack('u1u3u4s16', 1, 2, 3, -4)
bytearray(b'\xa3\xff\xfc')
>>> unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
(1, 2, 3, -4)
>>> calcsize('u1u3u4s16')
24

The unpacked fields can be named by assigning them to variables or by wrapping the result in a named tuple:

>>> from bitstruct import *
>>> from collections import namedtuple
>>> MyName = namedtuple('myname', [ 'a', 'b', 'c', 'd' ])
>>> unpacked = unpack('u1u3u4s16', bytearray(b'\xa3\xff\xfc'))
>>> myname = MyName(*unpacked)
>>> myname
myname(a=1, b=2, c=3, d=-4)
>>> myname.c
3

An example of packing/unpacking a unsinged integer, a signed integer, a float, a boolean and a bytearray:

>>> from bitstruct import *
>>> pack('u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
bytearray(b'\x0f\xd0\x1c\x00\x00?\xff')
>>> unpack('u5s5f32b1r13', bytearray(b'\x0f\xd0\x1c\x00\x00?\xff'))
(1, -1, 3.75, True, bytearray(b'\xff\xf8'))
>>> calcsize('u5s5f32b1r13')
56

An example of unpacking values from a hexstring and a binary file:

>>> from bitstruct import *
>>> from binascii import *
>>> unpack('s17s13r24', bytearray(unhexlify('0123456789abcdef')))
(582, -3751, bytearray(b'\xe2j\xf3'))
>>> with open("test.bin", "rb") as fin:
...     unpack('s17s13r24', bytearray(fin.read(8)))
...
...
(582, -3751, bytearray(b'\xe2j\xf3'))

Change endianness of the data with byteswap(), and then unpack the values:

>>> from bitstruct import *
>>> packed = pack('u1u3u4s16', 1, 2, 3, 1)
>>> unpack('u1u3u4s16', byteswap('12', packed))
(1, 2, 3, 256)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitstruct-2.0.1.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bitstruct-2.0.1-py2.py3-none-any.whl (5.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file bitstruct-2.0.1.tar.gz.

File metadata

  • Download URL: bitstruct-2.0.1.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bitstruct-2.0.1.tar.gz
Algorithm Hash digest
SHA256 632f7812e6559fb3e993bffe19e1033685bcbbe024e575d02c2a3097fbc887db
MD5 a9fdcf6f8c2b348693442ea435fdca51
BLAKE2b-256 71c7cfe8f87bc912ea3eb93a72ef11458468304d2ed55445f4a24122195b4612

See more details on using hashes here.

File details

Details for the file bitstruct-2.0.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for bitstruct-2.0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e5cf2ac7e4e3db5c6f624ae3050bfa543efd6542210e40d3d25e199f7b424775
MD5 ad4ff62886abc4346f704904ddd31272
BLAKE2b-256 92c03b67624997c4117434828601d00d887567f7058301c0efe6fbf6cf185192

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page