Previous topic

Next topic

Quick search

While it is not difficult to manipulate binary data in Python, for example using the struct and array modules, it can be quite fiddly and time consuming even for quite small tasks, especially if you are not dealing only with whole-byte data.

The bitstring module provides four classes, BitStream, BitArray, ConstBitStream and Bits, instances of which can be constructed from integers, floats, hex, octal, binary, strings or files, but they all just represent a string of binary digits. I shall use the general term ‘bitstring’ when referring generically to any of the classes, and use the class names for parts that apply to only one or another.

BitArray objects can be sliced, joined, reversed, inserted into, overwritten, packed, unpacked etc. with simple functions or slice notation. BitStream objects can also be read from, searched in, and navigated in, similar to a file or stream.

Bitstrings are designed to be as lightweight as possible and can be considered to be just a list of binary digits. They are however stored efficiently - although there are a variety of ways of creating and viewing the binary data, the bitstring itself just stores the byte data, and all views are calculated as needed, and are not stored as part of the object.

The different views or interpretations on the data are accessed through properties such as hex, bin and int, and an extensive set of functions is supplied for modifying, navigating and analysing the binary data.

A complete reference for the module is given in the Reference section, while the rest of this manual acts more like a tutorial or guided tour. Below are just a few examples to whet your appetite; everything here will be covered in greater detail in the rest of this manual.

frombitstringimportBitArray

Just some of the ways to create bitstrings:

# from a binary stringa=BitArray('0b001')# from a hexadecimal stringb=BitArray('0xff470001')# straight from a filec=BitArray(filename='somefile.ext')# from an integerd=BitArray(int=540,length=11)# using a format stringd=BitArray('int:11=540')

Easily construct new bitstrings:

# 5 copies of 'a' followed by two new bytese=5*a+'0xcdcd'# put a single bit on the fronte.prepend('0b1')# take a slice of the first 7 bitsf=e[7:]# replace 3 bits with 9 bits from octal stringf[1:4]='0o775'# find and replace 2 bit string with 16 bit stringf.replace('0b01','0xee34')

If you want an earlier version, or need other files in the full package, you can download and extract the contents of the .zip provided on the project’s website.

First download the latest release (see the Downloads tab on the project’s homepage). Note that this manual covers only the Python 2.6 and later version. Version 1.0 is available for Python 2.4 / 2.5, which can be found on the project’s homepage.

If you then extract the contents of the zip file you should find files organised in these directories

You might need to add a ‘sudo’ to the start of that command, depending on your system. This will copy the source files to your Python installation’s site-packages directory.

The module comes with comprehensive unit tests. To run them yourself use your favourite unit test running method, mine is:

nosetests -w test

which should run all the tests (over 400) and say OK. If tests fail then either your version of Python isn’t supported (you need Python 2.6, 2.7 or 3.x, though earlier versions supported 2.4 and 2.5) or something unexpected has happened - in which case please tell me about it.