Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to main content

Simple construction, analysis and modification of binary data.

Project description

bitstring

bitstring is a Python library 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.

The library has has now been downloaded over 100 million times!     ✨ Pepy Total Downloads

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


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

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 library is available. Some starting points are given below:

There is also an introductory walkthrough notebook on binder.

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 - 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

bitstring-4.3.1.tar.gz (251.4 kB view details)

Uploaded Source

Built Distribution

bitstring-4.3.1-py3-none-any.whl (71.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: bitstring-4.3.1.tar.gz
  • Upload date:
  • Size: 251.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for bitstring-4.3.1.tar.gz
Algorithm Hash digest
SHA256 a08bc09d3857216d4c0f412a1611056f1cc2b64fd254fb1e8a0afba7cfa1a95a
MD5 83984560794188b0f1183e115890dd91
BLAKE2b-256 15a8a80c890db75d5bdd5314b5de02c4144c7de94fd0cefcae51acaeb14c6a3f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bitstring-4.3.1-py3-none-any.whl
  • Upload date:
  • Size: 71.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for bitstring-4.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 69d1587f0ac18dc7d93fc7e80d5f447161a33e57027e726dc18a0a8bacf1711a
MD5 b7cbb71b88c7aca99c6dfa49926adaa1
BLAKE2b-256 752d174566b533755ddf8efb32a5503af61c756a983de379f8ad3aed6a982d38

See more details on using hashes here.

Supported by

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