cbitstruct - Fast C Bitstruct

1.2.0 · active · verified Fri Apr 17

cbitstruct is a Python library providing a faster C implementation of `bitstruct`. It offers API compatibility with `bitstruct`, allowing for efficient packing and unpacking of bitfields from and to Python data types. The current version is 1.2.0, and releases primarily focus on Python version support and bug fixes, with a focus on stability and performance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates packing and unpacking bitfields using `cbitstruct`. It's designed to be API compatible with the original `bitstruct` library, allowing for a drop-in replacement for performance-critical applications.

from cbitstruct import pack, unpack

# Pack 8 single-bit unsigned integers into a byte
data = pack('u1u1u1u1u1u1u1u1', 1, 0, 1, 0, 1, 0, 1, 0)
print(f"Packed data: {data!r}") # Expected: b'\xaa'

# Unpack the data back into a tuple of integers
unpacked_data = unpack('u1u1u1u1u1u1u1u1', data)
print(f"Unpacked data: {unpacked_data}") # Expected: (1, 0, 1, 0, 1, 0, 1, 0)

# Example with different format string
value = pack('u4u4', 0xA, 0x5)
print(f"Packed nibbles: {value!r}") # Expected: b'\xa5'

view raw JSON →