Skip to main content

A Python library for creating and parsing binary formats.

Project description

bitformat

A Python library for creating and parsing binary formats.

PyPI - Version CI badge Docs Codacy Badge


bitformat is a Python module for creating, manipulating and interpreting binary data. It also supports parsing and creating more complex binary formats.

It is from the author of the widely used bitstring module.


TLDR;

Here are a few code snippets to whet the appetite. If anything here looks useful to you then bitformat might be what you need.

Creating and manipulating bits:

    >>> from bitformat import MutableBits, Format, Array
    >>> b = MutableBits('0x123')
    >>> b += '0b110'
    >>> b.replace('0b11', '0xf')
    MutableBits('0b0001001000111111110')
    >>> b.unpack('(u15, [bool;])')
    (2335, (True, True, True, False))
    >>> list(b.to_bits().find_all('0b10'))
    [3, 6, 17]

Arrays of bits of a single data type:

    >>> a = Array.from_bits('i5', '0xfeed5')
    >>> a
    Array('i5', [-1, -5, -10, -11])
    >>> a + 10
    Array('i5', [9, 5, 0, -1])
    >>> a.dtype = 'bin4'
    >>> a
    Array('bin4', ['1111', '1110', '1110', '1101', '0101'])

Format creation and parsing:

    >>> f = Format("fmt: (x: u3, arr: [i4; {x}], tuple(f16, f32), repeat {x + 1}: bool)")
    >>> f.parse(b'some_byte_data')
    67
    >>> f.pp()
    fmt: (
        x: u3 = 3,
        arr: [i4; {x}] = (-7, -5, 7),
        tuple(f16, f32) = (-0.41845703125, -3.2239261260613716e-10),
        repeat{x + 1}:
            bool = False
            bool = False
            bool = True
            bool = True
    )
    >>> f['arr'].value = (1, 2, 3)
    >>> f.to_bytes()
    b'bGme_byt`'

Features

  • The Bits and MutableBits classes represent sequences of binary data of arbitrary length. They provide methods for creating, modifying and interpreting the data.
  • The Format class provides a way to define a binary format using a simple and flexible syntax.
  • A wide array of data types is supported, with no arbitrary restrictions on length.
  • Data is always stored as a contiguous array of bits.
  • The core is written in Rust for maximum performance.

[!NOTE] To see what has been added, improved or fixed, and also to see what's coming in the next version, see the release notes.

Installation

There should be pre-built wheels available for most platforms so you can just pip install bitformat. If a wheel isn't available then please file a bug report and optionally take a look at the build.sh script.

Documentation

Some Examples

Creating some Bits

A variety of constructor methods are available to create Bits, including from binary, hexadecimal or octal strings, formatted strings, byte literals and iterables.

>>> from bitformat import *

>>> a = Bits('0b1010')  # Create from a binary string
>>> b = Bits('u12 = 54')  # Create from a formatted string.
>>> c = Bits.from_bytes(b'\x01\x02\x03')  # Create from a bytes or bytearray object.
>>> d = Bits.from_dtype('f16', -0.75)  # Pack a value into a data type.
>>> e = Bits.from_joined([a, b, c, d])  # The best way to join lots of bits together.

Interpreting those Bits

Although the examples above were created from a variety of data types, the Bits instance doesn't retain any knowledge of how it was created - it's just a sequence of bits. You can therefore interpret them however you'd like:

>>> a.i
-6
>>> b.hex
'036'
>>> c.unpack(['u4', 'f16', 'u4'])
[0, 0.0005035400390625, 3]
>>> d.bytes
b'\xba\x00'

The unpack method is available as a general-case way to unpack the bits into a single or multiple data types. If you only want to unpack to a single data type you can use properties of the Bits as a short-cut.

Data types

A wide range of data types are supported. These are essentially descriptions on how binary data can be converted to a useful value. The Dtype class is used to define these, but usually just the string representation can be used.

Some example data type strings are:

  • 'u3' - a 3 bit unsigned integer.
  • 'i32_le' - a 32 bit little-endian signed integer.
  • 'f64' - a 64 bit IEEE float. Lengths of 16, 32 and 64 are supported.
  • 'bool' - a single bit boolean value.
  • 'bytes10' - a 10 byte sequence.
  • 'hex' - a hexadecimal string.
  • 'bin' - a binary string.
  • '[u8; 40]' - an array of 40 unsigned 8 bit integers.

Byte endianness for floating point and integer data types is specified with _le, _be and _ne suffixes to the base type.

Bit operations

An extensive set of operations are available to query Bits or to create new ones. For example:

>>> a + b  # Concatenation
Bits('0xa036')
>>> c.find('0b11')  # Returns found bit position
22
>>> b.replace('0b1', '0xfe')
Bits('0x03fbf9fdfc')
>>> b[0:10] | d[2:12]  # Slicing and logical operators
Bits('0b1110101101')

Arrays

An Array class is provided which stores a contiguous sequence of Bits of the same data type. This is similar to the array type in the standard module of the same name, but it's not restricted to just a dozen or so types.

>>> r = Array('i5', [4, -3, 0, 1, -5, 15])  # An array of 5 bit signed ints
>>> r -= 2  # Operates on each element
>>> r.to_list()
[2, -5, -2, -1, -7, 13]
>>> r.dtype = 'u6'  # You can freely change the data type
>>> r
Array('u6', [5, 47, 55, 60, 45])
>>> r.to_bits()
Bits('0b000101101111110111111100101101')

A Format example

The Format class can be used to give structure to bits, as well as storing the data in a human-readable form.

>>> f = Format('(width: u12, height: u12, flags: [bool; 4])')
>>> f.pack([320, 240, [True, False, True, False]])
>>> print(f)
(
    width: u12 = 320
    height: u12 = 240
    flags: [bool; 4] = (True, False, True, False)
)
>>> f['height'].value /= 2
>>> f.to_bits()
Bits('0x140078a')
>>> f.to_bits() == 'u12=320, u12=120, 0b1010'
True

The Format and its fields can optionally have names (the Format above is unnamed, but its fields are named). In this example the pack method was used with appropriate values, which then returned a Bits object. The Format now contains all the interpreted values, which can be easily accessed and modified.

The final line in the example above demonstrates how new Bits objects can be created when needed by promoting other types, in this case the formatted string is promoted to a Bits object before the comparison is made.

The Format can be used symmetrically to both create and parse binary data:

>>> f.parse(b'x\x048\x10')
28
>>> f
Format([
    'width: u12 = 1920',
    'height: u12 = 1080',
    'flags: [bool; 4] = (False, False, False, True)'
])

The parse method is able to lazily parse the input bytes, and simply returns the number of bits that were consumed. The actual values of the individual fields aren't calculated until they are needed, which allows large and complex file formats to be efficiently dealt with.

More to come

The bitformat library is still in beta and is being actively developed. The first release was in September 2024, with the second arriving in January 2025 and the third in April. Version 0.5 was released in June 2025. More features are being added steadily.

There are a number of important features planned, some of which are from the bitstring library on which much of the core is based, and others are needed for a full binary format experience.

The (unordered) :todo: list includes:

  • Streaming methods. :sparkles: Done in v0.2 - see the Reader class :sparkles: .
  • Field expressions. :sparkles: Done in v0.3 :sparkles: Rather than hard-coding everything in a field, some parts will be calculated during the parsing process. For example in the format '(w: u16, h: u16, [u8; {w * h}])' the size of the 'u8' array would depend on the values parsed just before it.
  • New field types. :sparkles: Done in v0.3 :sparkles: Fields like Repeat and If are planned which will allow more flexible formats to be written.
  • Exotic floating point types. In bitstring there are a number of extra floating point types such as bfloat and the MXFP 8, 6 and 4-bit variants. These will be ported over to bitformat.
  • Performance improvements. A primary focus on the design of bitformat is that it should be fast. Version 0.5 received some significant speed boosts and is now competitive for most use cases.
  • LSB0. Currently all bit positions are done with the most significant bit being bit zero (MSB0). I plan to add support for least significant bit zero (LSB0) bit numbering as well.

Copyright (c) 2024-2025 Scott Griffiths

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

bitformat-0.9.0.tar.gz (3.7 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

bitformat-0.9.0-cp314-cp314t-win_amd64.whl (335.7 kB view details)

Uploaded CPython 3.14tWindows x86-64

bitformat-0.9.0-cp314-cp314t-win32.whl (318.8 kB view details)

Uploaded CPython 3.14tWindows x86

bitformat-0.9.0-cp311-abi3-win_amd64.whl (332.9 kB view details)

Uploaded CPython 3.11+Windows x86-64

bitformat-0.9.0-cp311-abi3-win32.whl (318.4 kB view details)

Uploaded CPython 3.11+Windows x86

bitformat-0.9.0-cp311-abi3-musllinux_1_2_x86_64.whl (540.2 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ x86-64

bitformat-0.9.0-cp311-abi3-musllinux_1_2_ppc64le.whl (537.6 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ ppc64le

bitformat-0.9.0-cp311-abi3-musllinux_1_2_aarch64.whl (511.7 kB view details)

Uploaded CPython 3.11+musllinux: musl 1.2+ ARM64

bitformat-0.9.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (458.0 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ x86-64

bitformat-0.9.0-cp311-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl (495.5 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ppc64le

bitformat-0.9.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl (446.7 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ ARM64

bitformat-0.9.0-cp311-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl (485.8 kB view details)

Uploaded CPython 3.11+manylinux: glibc 2.17+ i686manylinux: glibc 2.5+ i686

bitformat-0.9.0-cp311-abi3-macosx_11_0_arm64.whl (416.9 kB view details)

Uploaded CPython 3.11+macOS 11.0+ ARM64

bitformat-0.9.0-cp311-abi3-macosx_10_12_x86_64.whl (446.5 kB view details)

Uploaded CPython 3.11+macOS 10.12+ x86-64

File details

Details for the file bitformat-0.9.0.tar.gz.

File metadata

  • Download URL: bitformat-0.9.0.tar.gz
  • Upload date:
  • Size: 3.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bitformat-0.9.0.tar.gz
Algorithm Hash digest
SHA256 a58e185a512b8ef1dea79512584e3f64d2b56deffccd6e6a0a17340bc328404f
MD5 afed48ee7e470134803e36ab9b5a2123
BLAKE2b-256 eee420f92ba853c6e5a70dc49c88c4342c958352d2128108742a0ca5a0b65dde

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp314-cp314t-win_amd64.whl.

File metadata

  • Download URL: bitformat-0.9.0-cp314-cp314t-win_amd64.whl
  • Upload date:
  • Size: 335.7 kB
  • Tags: CPython 3.14t, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bitformat-0.9.0-cp314-cp314t-win_amd64.whl
Algorithm Hash digest
SHA256 b072ab2751775e63b5694c20c324ea1351aec15525fea121ace01439873798e3
MD5 c6b63930c715462b76ac4ffc09c060be
BLAKE2b-256 83a36ecad96adeaa2632424c79a34eadefcf9de0a69df4e1ce636ef2e841fa7c

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp314-cp314t-win32.whl.

File metadata

  • Download URL: bitformat-0.9.0-cp314-cp314t-win32.whl
  • Upload date:
  • Size: 318.8 kB
  • Tags: CPython 3.14t, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bitformat-0.9.0-cp314-cp314t-win32.whl
Algorithm Hash digest
SHA256 4e20917d57c67449b482b2cae687f824873b6d870d41c2eb76c5b41d2d13f9c5
MD5 5cdb30d8a0ec3ac143540028964fc0b9
BLAKE2b-256 8489743506851bff4c19e910343bb59a0bbe1391698d3613cd0d0052e69afcf4

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-win_amd64.whl.

File metadata

  • Download URL: bitformat-0.9.0-cp311-abi3-win_amd64.whl
  • Upload date:
  • Size: 332.9 kB
  • Tags: CPython 3.11+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 6c912ab302435f94dc2260bc8eebd29250404de7823d5fb7af1ff25c8f755f91
MD5 6d47e386dd02cab2467f45dedb72c161
BLAKE2b-256 4f2e8d7c9f1bdda3c8291cc1a6d38f8db859c925ed99abac972677bfb0fccd22

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-win32.whl.

File metadata

  • Download URL: bitformat-0.9.0-cp311-abi3-win32.whl
  • Upload date:
  • Size: 318.4 kB
  • Tags: CPython 3.11+, Windows x86
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-win32.whl
Algorithm Hash digest
SHA256 b630046257e2083cf4bff60dde2bf3d50214ab62b9d3bacb514bea21cb8f6e5a
MD5 380abfd81bb8f1dceca74aeba22bcc0b
BLAKE2b-256 253521ffd7ad6672f51a79491e412134ec0362e28978ce1e8acc52f583ba154c

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 cb7fc5987d9448e7dca953a38fcd1d1338bf4ecf8ff26684a09de176aa1948a3
MD5 cf23dbb8e740fcef9d9b9a09cc054748
BLAKE2b-256 6bb70d96e2f67978823bbaa527f71c5e7757492626b56f1ba9c5c0504230d3ce

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-musllinux_1_2_ppc64le.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-musllinux_1_2_ppc64le.whl
Algorithm Hash digest
SHA256 fd9590209e37b82f27f672886fa96dc221d934b56f63b03e586abb5c1e89fb71
MD5 0cf98618a58bbbfd2e8897c4504f00f7
BLAKE2b-256 2d293991186af8b6b4f2ea694fb4fde2073bdae4ef0c57533bae4a2670b35ebf

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 4fae0990743b6ddf21b69fd6ff11f6beaf569d6cb3c0ce40a4703140106fb5d2
MD5 9cfc46dd1eff7d08d2adb3b965cbd23a
BLAKE2b-256 ae71be640210a1b48e8477a4d9f80409589f3fdaeee2332d8e20da78428d7495

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 e4fc05a96765eae303a479f2f91f4b7cde4846679b0929467ef4533c1060f0a8
MD5 551743a6a16b067064bbf68ff9b094e1
BLAKE2b-256 cae9c7fa13fd8923afae3c654b1fcaf92f88fd8812a8c690d08e71bae306d33d

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-manylinux2014_ppc64le.manylinux_2_17_ppc64le.whl
Algorithm Hash digest
SHA256 560f8c327fba15fb9a578d5926e96b7814a665bf0461684ded769f56967cbc29
MD5 ca671310f6e969de61dcbd47d6e9aec0
BLAKE2b-256 78d3542b0564451f5e9e2d644b2754342d7e70442ee669349b2340de89321855

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 e784925d5e2d8dda54e4172f41547444e0dd249bcc45cefb13351ead6335b320
MD5 5bd5a9348a2508d0e05c5dbd7415099f
BLAKE2b-256 5bea29e433d554e87e5a00b6a13c3f2f2283ac535bddbd21903005cfba3a3468

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-manylinux1_i686.manylinux2014_i686.manylinux_2_17_i686.manylinux_2_5_i686.whl
Algorithm Hash digest
SHA256 2368fcacb79703cf40cf35f1f37b3fea593e2ec3b227dc70cb443ae8e2987cc8
MD5 576c2e59f5946c8892baca7a583e7c7a
BLAKE2b-256 976165af9e08b3becbe9047a5edb44d8f758ac78b1f5157e45ac2b71e9260772

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 add22ad080307d74cd115c38eee0d4af7e01869f35faf29e4aab3185b3025f5f
MD5 afa7f5b34b6b09985c7f6ffdd4be587a
BLAKE2b-256 06c9b6a2504dec56ab741bb0dab7e7270f4906b495f0c292b732d9391f90f8d4

See more details on using hashes here.

File details

Details for the file bitformat-0.9.0-cp311-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for bitformat-0.9.0-cp311-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4e8eab73e2b0f09445c804bca38d831ad635e846101290c109af90e766bc83c8
MD5 a4a66af78a4d1c9b4371dbfedc1382bc
BLAKE2b-256 29924f29c9ad1016ffa7226e520a2234efb68b7219afc98a3d97566fb68cd99b

See more details on using hashes here.

Supported by

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