Skip to main content

Simple construction, analysis and modification of binary data.

Project description

/doc/bitstring_logo_small.png

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

Bitstrings can be constructed from integers (big and little endian), hex, octal, binary, strings or files. They can be sliced, joined, reversed, inserted into, overwritten, etc. with simple functions or slice notation. They can also be read from, searched and replaced, and navigated in, similar to a file or stream.

bitstring is open source software, and has been released under the MIT licence.

This module works in both Python 2.7 and Python 3.6+.

Installation

Probably all you need to do is:

pip install bitstring

Documentation

The manual for the bitstring module is available here <https://bitstring.readthedocs.org>. It contains a walk-through of all the features and a complete reference section.

It is also available as a PDF at <https://readthedocs.org/projects/bitstring/downloads/pdf/latest/>.

Simple Examples

Creation:

>>> a = BitArray(bin='00101')
>>> b = Bits(a_file_object)
>>> c = BitArray('0xff, 0b101, 0o65, uint:6=22')
>>> d = pack('intle:16, hex=a, 0b1', 100, a='0x34f')
>>> e = pack('<16h', *range(16))

Different interpretations, slicing and concatenation:

>>> a = BitArray('0x1af')
>>> a.hex, a.bin, a.uint
('1af', '000110101111', 431)
>>> a[10:3:-1].bin
'1110101'
>>> '0b100' + 3*a
BitArray('0x835e35e35, 0b111')

Reading data sequentially:

>>> b = BitStream('0x160120f')
>>> b.read(12).hex
'160'
>>> b.pos = 0
>>> b.read('uint:12')
352
>>> b.readlist('uint:12, bin:3')
[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]

Unit Tests

The 500+ unit tests should all pass for Python 2.7 and later. To run them, from the test directory run:

python -m unittest discover

The bitstring module has been released as open source under the MIT License. Copyright (c) 2006 Scott Griffiths

For more information see the project’s homepage on GitHub: <https://github.com/scott-griffiths/bitstring>

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-3.1.9.tar.gz (198.5 kB view details)

Uploaded Source

Built Distributions

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

bitstring-3.1.9-py3-none-any.whl (38.4 kB view details)

Uploaded Python 3

bitstring-3.1.9-py2-none-any.whl (38.4 kB view details)

Uploaded Python 2

File details

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

File metadata

  • Download URL: bitstring-3.1.9.tar.gz
  • Upload date:
  • Size: 198.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.9

File hashes

Hashes for bitstring-3.1.9.tar.gz
Algorithm Hash digest
SHA256 a5848a3f63111785224dca8bb4c0a75b62ecdef56a042c8d6be74b16f7e860e7
MD5 04d24380a0dc491e6bdacd983a90912e
BLAKE2b-256 4cb180d58eeb21c9d4ca739770558d61f6adacb13aa4908f4f55e0974cbd25ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bitstring-3.1.9-py3-none-any.whl
  • Upload date:
  • Size: 38.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.9

File hashes

Hashes for bitstring-3.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0de167daa6a00c9386255a7cac931b45e6e24e0ad7ea64f1f92a64ac23ad4578
MD5 635135d5ed3ed00e080b1a95748ee911
BLAKE2b-256 49faac153ef3c9668a093f33386edf7a20122962e9142b1105fbe2a4a4262785

See more details on using hashes here.

File details

Details for the file bitstring-3.1.9-py2-none-any.whl.

File metadata

  • Download URL: bitstring-3.1.9-py2-none-any.whl
  • Upload date:
  • Size: 38.4 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.9

File hashes

Hashes for bitstring-3.1.9-py2-none-any.whl
Algorithm Hash digest
SHA256 e3e340e58900a948787a05e8c08772f1ccbe133f6f41fe3f0fa19a18a22bbf4f
MD5 85d62e5f244705fcad305394dd4523f7
BLAKE2b-256 2adec831618fed7911cb5252de2cccd01fd7894574b4419be05aa6c125083de5

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