Skip to main content

Simple construction, analysis and modification of binary data.

Project description

bitstring

bitstring is a Python module to help make the creation and analysis of binary data as simple and efficient as possible.

It has been maintained since 2006 and now has many millions of downloads per year.

CI badge Docs Codacy Badge Downloads     Binder

News

17th August 2023: bitstring 4.1.0 released. This is a very big update in terms of how much of the code has changed.

  • Speed increased with bitarray dependency.
  • New Array class for homogeneous data.
  • Support for 8-bit floating point values.

See the release notes for details. Please let me know if you encounter any problems.

Overview

  • Efficiently store and manipulate binary data in idiomatic Python.
  • Create bitstrings from hex, octal, binary, files, formatted strings, bytes, integers and floats of different endiannesses.
  • Powerful binary packing and unpacking functions.
  • Bit-level slicing, joining, searching, replacing and more.
  • Create and manipulate arrays of fixed-length bitstrings.
  • Read from and interpret bitstrings as streams of binary data.
  • Rich API - chances are that whatever you want to do there's a simple and elegant way of doing it.
  • Open source software, released under the MIT licence.

Documentation

Extensive documentation for the bitstring module is available. Some starting points are given below:

You can also try out the interactive walkthrough notebook on binder.

Release Notes

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

Examples

Installation

$ pip install bitstring

Creation

 >>> from bitstring import Bits, BitArray, BitStream, pack
 >>> a = BitArray(bin='00101')
 >>> b = Bits(a_file_object)
 >>> c = BitArray('0xff, 0b101, 0o65, uint6=22')
 >>> d = pack('intle16, hex=a, 0b1', 100, a='0x34f')
 >>> e = pack('<16h', *range(16))

Different interpretations, slicing and concatenation

 >>> a = BitArray('0x3348')
 >>> a.hex, a.bin, a.uint, a.float, a.bytes
 ('3348', '0011001101001000', 13128, 0.2275390625, b'3H')
 >>> a[10:3:-1].bin
 '0101100'
 >>> '0b100' + 3*a
 BitArray('0x866906690669, 0b000')

Reading data sequentially

 >>> b = BitStream('0x160120f')
 >>> b.read(12).hex
 '160'
 >>> b.pos = 0
 >>> b.read('uint12')
 352
 >>> b.readlist('uint12, bin3')
 [288, '111']

Searching, inserting and deleting

 >>> c = BitArray('0b00010010010010001111')   # c.hex == '0x1248f'
 >>> c.find('0x48')
 (8,)
 >>> c.replace('0b001', '0xabc')
 >>> c.insert('0b0000', pos=3)
 >>> del c[12:16]

Arrays of fixed-length formats

 >>> from bitstring import Array
 >>> a = Array('uint7', [9, 100, 3, 1])
 >>> a.data
 BitArray('0x1390181')
 >>> a[::2] *= 5
 >>> a
 Array('uint7', [45, 100, 15, 1])

Unit Tests

The 700+ unit tests should all pass. They can be run from the root of the project with

 python -m unittest

Credits

Created by Scott Griffiths in 2006 to help with ad hoc parsing and creation of compressed video files. Maintained and expanded ever since as it became unexpectedly popular. Thanks to all those who have contributed ideas and code (and bug reports) over the years.

Copyright (c) 2006 - 2023 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

bitstring-4.1.2.tar.gz (232.9 kB view details)

Uploaded Source

Built Distribution

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

bitstring-4.1.2-py3-none-any.whl (60.0 kB view details)

Uploaded Python 3

File details

Details for the file bitstring-4.1.2.tar.gz.

File metadata

  • Download URL: bitstring-4.1.2.tar.gz
  • Upload date:
  • Size: 232.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for bitstring-4.1.2.tar.gz
Algorithm Hash digest
SHA256 c22283d60fd3e1a8f386ccd4f1915d7fe13481d6349db39711421e24d4a9cccf
MD5 1750c35e8e45f597725037d35eed9ba1
BLAKE2b-256 23fcb5ace4f51fea5bcc7f8cca8859748ea5eb941680b82a5b3687c980d9589b

See more details on using hashes here.

File details

Details for the file bitstring-4.1.2-py3-none-any.whl.

File metadata

  • Download URL: bitstring-4.1.2-py3-none-any.whl
  • Upload date:
  • Size: 60.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for bitstring-4.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bc0dbdb282099d5c6fbf995cd7261449ddeb7bbf042996e503471a35eb2efaa3
MD5 54e93cb45101f7c6adb9bf5647e1cd83
BLAKE2b-256 59bcbbc41ad3546f23855a2c21dc6afcd8b148ccec2e51a5af145199abfa4b9e

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