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)
Metadata
Release files for bitstruct 2.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bitstruct-2.0.2.tar.gz | 3.8 kB | Without provenance | Details for bitstruct-2.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bitstruct-2.0.2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for bitstruct-2.0.2-py2.py3-none-any.whl |
Total release size: 9.4 kB
Release files / bitstruct-2.0.2.tar.gz
| Download URL | bitstruct-2.0.2.tar.gz |
|---|---|
| Size | 3.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0166c67057db54b94b9bf58f8ea999dc311eaba8cbf3a099214fc6eaaaf80a4b
|
|
BLAKE2b-256 checksum How to use checksums |
95c99b86eb1008923bc77fce0129279f04cc20573807d11cbf945376a2a7adf4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / bitstruct-2.0.2-py2.py3-none-any.whl
| Download URL | bitstruct-2.0.2-py2.py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
eb7ed9b3891dbe1e3903427f17d56f699572ef325fd784ec8a34d6e952255a77
|
|
BLAKE2b-256 checksum How to use checksums |
1549fb02b74e13f8bf3bf13ed9b18038e3383e05c8926d376c9b026f02203c8b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for bitstruct 2.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.