This module performs conversions between Python values and C bit field structs represented as Python byte strings.
Project description
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)
b'\xa3\xff\xfc'
>>> unpack('u1u3u4s16', 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', 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, a byte string and a string:
>>> from bitstruct import *
>>> pack('u5s5f32b1r13t40', 1, -1, 3.75, True, b'\xff\xff', u'hello')
b'\x0f\xd0\x1c\x00\x00?\xffhello'
>>> unpack('u5s5f32b1r13t40', b'\x0f\xd0\x1c\x00\x00?\xffhello')
(1, -1, 3.75, True, b'\xff\xf8', u'hello')
>>> calcsize('u5s5f32b1r13t24')
80
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, b'\xff\xff')
b'\x87\xc0\x00\x03\x80\xbf\xff'
>>> unpack('<u5s5f32b1r13', b'\x87\xc0\x00\x03\x80\xbf\xff')
(1, -1, 3.75, True, b'\xff\xf8')
>>> calcsize('<u5s5f32b1r13')
56
An example of unpacking values from a hexstring and a binary file:
>>> from bitstruct import *
>>> unpack('s17s13r24', '0123456789abcdef'.decode('hex'))
(582, -3751, b'\xe2j\xf3')
>>> with open("test.bin", "rb") as fin:
... unpack('s17s13r24', fin.read(8))
...
...
(582, -3751, 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file bitstruct-3.3.0.tar.gz
.
File metadata
- Download URL: bitstruct-3.3.0.tar.gz
- Upload date:
- Size: 15.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1f43e5cddb018da389685bbd007b1a71ed8ed113608ce4c9c1819a2326dbd20b |
|
MD5 | 6be999700aca1f28452c597e52b2bb1c |
|
BLAKE2b-256 | 6177aeaf49caa8d31778405a1a15fcac8bf69477218e016214c4dc524b5972ec |
Provenance
File details
Details for the file bitstruct-3.3.0-py2.py3-none-any.whl
.
File metadata
- Download URL: bitstruct-3.3.0-py2.py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ebc8d66c36770e06310a7e9b9bae136e7dd130056ca73d3e570c99792a1ea59 |
|
MD5 | 3854c3fbb5a879aa6e2c78c04758531a |
|
BLAKE2b-256 | 720733f1158096e974cbcc3d5443ec79a6f1db43f03c64bfa96c85c8a6fb96a3 |