This release is a pre-release and may not be stable for production use.
bitstring is a pure Python module designed to help make the creation and analysis of binary data as simple and natural as possible.
It has been maintained since 2006 and now has about 20 million downloads per year.
bitstring version 4.0 only supports Python 3.7 and later. Use bitstring version 3.1 if you're using Python 2.7 or 3.6 or earlier.
Overview
- 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.
- 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
The manual for the bitstring module is available at Read the Docs. It contains a walk-through of all the features and a complete reference section.
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 600+ 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 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bitstring-4.0.0b3.tar.gz.
File metadata
- Download URL: bitstring-4.0.0b3.tar.gz
- Upload date:
- Size: 172.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/4.0.1 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bc259b8dac8d21ecdb3c6eb562aeee63230cbb50c4c4ab0db73dd2fd0569127e
|
|
| MD5 |
d4acea0ad96f33046a97110cc2280712
|
|
| BLAKE2b-256 |
7f1ede9160033f3d26a7492efbe4e5715242a77c5888a47004083a8a7f6041a1
|
File details
Details for the file bitstring-4.0.0b3-py3-none-any.whl.
File metadata
- Download URL: bitstring-4.0.0b3-py3-none-any.whl
- Upload date:
- Size: 44.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/4.0.1 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16ca5844970e4567e822931c01b08b266e406b7ea5fcdc1f091dbe1837d4afcf
|
|
| MD5 |
cdbbb2c034ef85426b59d420abbb3c4c
|
|
| BLAKE2b-256 |
8f489e36f2a6397de5e9dc6c8463101d384524ac9eb410db1d667726a18ee75a
|