Skip to main content

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)

Release files for bitstruct 3.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for bitstruct 3.0.0
File Interpreter ABI Platform
bitstruct-3.0.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Release files / bitstruct-3.0.0-py2.py3-none-any.whl

Download URL bitstruct-3.0.0-py2.py3-none-any.whl
Size 6.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
fe08548c090c4b68aa7e9ba4dbba27d7cf66b116652ee299756ab7eea9d10cae
BLAKE2b-256 checksum
How to use checksums
d42516d6687aee0492956c88eecd5cef058bd1a825734d97feb6629faa56c0b7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

8.17.0

1 release file

8.16.1

1 release file

8.15.1

1 release file

8.15.0

1 release file

8.14.1

1 release file

8.14.0

1 release file

8.13.0

1 release file

8.12.1

1 release file

8.12.0

1 release file

8.11.1

1 release file

8.11.0

1 release file

8.10.0

1 release file

8.9.0

1 release file

8.8.1

1 release file

8.8.0

1 release file

8.7.3

1 release file

8.7.2

1 release file

8.7.1

1 release file

8.7.0

1 release file

8.6.0

1 release file

8.5.1

1 release file

8.5.0

1 release file

8.4.0

1 release file

8.3.1

1 release file

8.3.0

1 release file

8.2.0

1 release file

8.1.2

1 release file

8.1.1

2 release files

8.1.0

2 release files

8.0.0

2 release files

7.1.0

2 release files

6.0.0

2 release files

5.2.1

2 release files

5.2.0

2 release files

5.1.0

2 release files

5.0.0

2 release files

4.1.0

2 release files

4.0.0

2 release files

3.10.0

2 release files

3.9.0

2 release files

3.8.0

2 release files

3.7.0

2 release files

3.6.1

2 release files

3.6.0

2 release files

3.5.0

2 release files

3.4.0

2 release files

3.3.1

2 release files

3.3.0

2 release files

3.2.0

2 release files

3.1.0

2 release files

This release

3.0.0 This release

1 release file

2.1.4

2 release files

2.1.3

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page