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

The same format and values as in the previous example, but using LSB (Least Significant Bit) first instead of the default MSB (Most Significant Bit) first:

>>> from bitstruct import *
>>> pack('<u5s5f32b1r13', 1, -1, 3.75, True, bytearray(b'\xff\xff'))
bytearray(b'\x87\xc0\x00\x03\x80\xbf\xff')
>>> unpack('<u5s5f32b1r13', bytearray(b'\x87\xc0\x00\x03\x80\xbf\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 *
>>> unpack('s17s13r24', bytearray('0123456789abcdef'.decode('hex')))
(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.1.2.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

bitstruct-2.1.2-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for bitstruct-2.1.2.tar.gz
Algorithm Hash digest
SHA256 5e4c929686e50f6f68bb2b46a6c9d10738cf32bd2a5fd3d66119dcc186b640b7
MD5 bb3b83fa8cb69e410e9234cf1c696afc
BLAKE2b-256 7d3c13eebf78933d4e892d1cf7929c90322a0e31220fbd200573aa438d5a5fb8

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for bitstruct-2.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f32b7b48fffd3d8b7679507945b0c4ea83ba70495f5417b8ed9e327d2ed8720e
MD5 5a1838beece7b24a80f399d3d90dd554
BLAKE2b-256 bd57debf5a8dd12f48283a83b00aeb99204bf18c6cac701d61445838ae7ab6f5

See more details on using hashes here.

Provenance

Supported by

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