Skip to main content

bitstring

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

It has been actively maintained since 2006.

CI badge Docs Codacy Badge Dependents (via libraries.io)     Pepy Total Downlods PyPI - Downloads

News

May 2024: bitstring 4.2.2 released.

New in version 4.2:

  • Dropped support for Python 3.7. Minimum version is now 3.8.
  • A new Dtype class can be optionally used to specify types.
  • The bitstring.options object is now the preferred method for changing module options.
  • New fromstring method as another way to create bitstrings from formatted strings.
  • More types can now be pretty printed.
  • A range of 8-bit, 6-bit and even 4-bit floating point formats added (beta):
  • Performance improvements.

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:

There is also an introductory 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])

Copyright (c) 2006 - 2024 Scott Griffiths

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.2.2.tar.gz (249.4 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.2.2-py3-none-any.whl (71.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bitstring-4.2.2.tar.gz
  • Upload date:
  • Size: 249.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for bitstring-4.2.2.tar.gz
Algorithm Hash digest
SHA256 b40b01d911eebaea6efff40d826580806dced5e04b9d3cbad6aebf9422f4b643
MD5 0e98bc895e8a04cd5af0b3789283d664
BLAKE2b-256 2d1ff0ba279ed3291133921ccace1f8500031a23a129870b8220f1cc509186a4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bitstring-4.2.2-py3-none-any.whl
  • Upload date:
  • Size: 71.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.6

File hashes

Hashes for bitstring-4.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8b784373e78e953879c8192589e1ecbcda8f111841633a2aaf88d2b073fd6c35
MD5 16b60ffe89520225bada91ca50587b27
BLAKE2b-256 a0e917a2ef211c6c84b6704213e4ee954561529f6a4746dbbd6081b83d6d4d72

See more details on using hashes here.

Release history Release notifications | RSS feed

4.4.0

2 files

4.3.1

2 files

4.3.0

2 files

4.2.3

2 files

This release

4.2.2 This release

2 files

4.2.1

2 files

4.2.0

2 files

4.1.4

2 files

4.1.3

2 files

4.1.2

2 files

4.1.1

2 files

4.1.0

2 files

4.0.2

2 files

4.0.1

2 files

4.0.0

2 files

4.0.0b1

2 files

3.1.9

3 files

3.1.8

3 files

3.1.7

1 file

3.1.6

3 files

3.1.5

1 file

3.1.4

1 file

3.1.3

3.1.2

1 file

3.1.1

1 file

3.1.0

1 file

3.0.2

1 file

3.0.1

1 file

3.0.0

1 file

2.2.0

1 file

2.1.1

1 file

2.1.0

1 file

2.0.3

1 file

2.0.2

1 file

1.3.0

1 file

1.2.0

1 file

1.1.3

1 file

1.1.2

1 file

1.1.1

1 file

1.1.0

1 file

1.0.0

1 file

0.5.2

2 files

0.5.1

2 files

0.5.0

2 files

0.4.3

1 file

0.4.2

1 file

0.4.0

2 files

0.3.2

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page