Skip to main content

buildstatus coverage

About

This module is intended to have a similar interface as the python struct module, but working on bits instead of primitive data types (char, int, …).

Project homepage: https://github.com/eerimoq/bitstruct

Documentation: http://bitstruct.readthedocs.org/en/latest

Installation

pip install bitstruct

Example usage

A basic example of packing and unpacking four integers using the format string 'u1u3u4s16':

>>> from bitstruct import *
>>> pack('u1u3u4s16', 1, 2, 3, -4)
b'\xa3\xff\xfc'
>>> unpack('u1u3u4s16', b'\xa3\xff\xfc')
(1, 2, 3, -4)
>>> calcsize('u1u3u4s16')
24

An example compiling the format string once, and use it to pack and unpack data:

>>> import bitstruct
>>> cf = bitstruct.compile('u1u3u4s16')
>>> cf.pack(1, 2, 3, -4)
b'\xa3\xff\xfc'
>>> cf.unpack(b'\xa3\xff\xfc')
(1, 2, 3, -4)

The unpacked values can be named by assigning them to variables or by wrapping the result in a named tuple:

>>> from bitstruct import *
>>> from collections import namedtuple
>>> MyName = namedtuple('myname', [ 'a', 'b', 'c', 'd' ])
>>> unpacked = unpack('u1u3u4s16', b'\xa3\xff\xfc')
>>> myname = MyName(*unpacked)
>>> myname
myname(a=1, b=2, c=3, d=-4)
>>> myname.c
3

An example of packing and unpacking an unsinged integer, a signed integer, a float, a boolean, a byte string and a string:

>>> from bitstruct import *
>>> pack('u5s5f32b1r13t40', 1, -1, 3.75, True, b'\xff\xff', 'hello')
b'\x0f\xd0\x1c\x00\x00?\xffhello'
>>> unpack('u5s5f32b1r13t40', b'\x0f\xd0\x1c\x00\x00?\xffhello')
(1, -1, 3.75, True, b'\xff\xf8', 'hello')
>>> calcsize('u5s5f32b1r13t40')
96

The same format string and values as in the previous example, but using LSB (Least Significant Bit) first instead of the default MSB (Most Significant Bit) first:

>>> from bitstruct import *
>>> pack('<u5s5f32b1r13t40', 1, -1, 3.75, True, b'\xff\xff', 'hello')
b'\x87\xc0\x00\x03\x80\xbf\xff\xf666\xa6\x16'
>>> unpack('<u5s5f32b1r13t40', b'\x87\xc0\x00\x03\x80\xbf\xff\xf666\xa6\x16')
(1, -1, 3.75, True, b'\xff\xf8', 'hello')
>>> calcsize('<u5s5f32b1r13t40')
96

An example of unpacking values from a hexstring and a binary file:

>>> from bitstruct import *
>>> from binascii import unhexlify
>>> unpack('s17s13r24', unhexlify('0123456789abcdef'))
(582, -3751, b'\xe2j\xf3')
>>> with open("test.bin", "rb") as fin:
...     unpack('s17s13r24', fin.read(8))
...
...
(582, -3751, b'\xe2j\xf3')

Change endianness of the data with byteswap, and then unpack the values:

>>> from bitstruct import *
>>> packed = pack('u1u3u4s16', 1, 2, 3, 1)
>>> unpack('u1u3u4s16', byteswap('12', packed))
(1, 2, 3, 256)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bitstruct-3.5.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

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

bitstruct-3.5.0-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file bitstruct-3.5.0.tar.gz.

File metadata

  • Download URL: bitstruct-3.5.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bitstruct-3.5.0.tar.gz
Algorithm Hash digest
SHA256 6a67f91eaec4aebb7e561c8a27b3c100140b9b18b6a57b293eed6cc4b8d24128
MD5 3221fa4de336f440cf0f8e01f98a8ae4
BLAKE2b-256 b291cd64871016cfa91f541d8335793449fbff7256df514f5baa880e4795efea

See more details on using hashes here.

File details

Details for the file bitstruct-3.5.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for bitstruct-3.5.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 68561e84ea112bcbc3042984495949653b1394e17a48496b46f22daff33de4c9
MD5 da59e7b8c90bd78c1bc31d59132eaf2c
BLAKE2b-256 095bda83322a4b0e4c5fe366501693461ddf4a1de3a871f36ac6fbbe58e6b3bd

See more details on using hashes here.

Release history Release notifications | RSS feed

8.23.0

100 files

8.22.2

100 files

8.22.1

78 files

8.22.0

64 files

8.21.0

61 files

8.20.0

49 files

8.19.0

43 files

8.18.0

43 files

8.17.0

1 file

8.16.1

1 file

8.15.1

1 file

8.15.0

1 file

8.14.1

1 file

8.14.0

1 file

8.13.0

1 file

8.12.1

1 file

8.12.0

1 file

8.11.1

1 file

8.11.0

1 file

8.10.0

1 file

8.9.0

1 file

8.8.1

1 file

8.8.0

1 file

8.7.3

1 file

8.7.2

1 file

8.7.1

1 file

8.7.0

1 file

8.6.0

1 file

8.5.1

1 file

8.5.0

1 file

8.4.0

1 file

8.3.1

1 file

8.3.0

1 file

8.2.0

1 file

8.1.2

1 file

8.1.1

2 files

8.1.0

2 files

8.0.0

2 files

7.1.0

2 files

6.0.0

2 files

5.2.1

2 files

5.2.0

2 files

5.1.0

2 files

5.0.0

2 files

4.1.0

2 files

4.0.0

2 files

3.10.0

2 files

3.9.0

2 files

3.8.0

2 files

3.7.0

2 files

3.6.1

2 files

3.6.0

2 files

This release

3.5.0 This release

2 files

3.4.0

2 files

3.3.1

2 files

3.3.0

2 files

3.2.0

2 files

3.1.0

2 files

3.0.0

1 file

2.1.4

2 files

2.1.3

2 files

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

2.0.2

2 files

2.0.1

2 files

2.0.0

2 files

1.1.0

2 files

1.0.1

2 files

1.0.0

2 files

Supported by

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