Fastest way to unpack bits (sub-byte) numbers from file

zachd1_618

Given a file with resolution-compressed binary data, I would like to convert the sub-byte bits into their integer representations in python. By this I mean I need to interpret n bits from a file as an integer.

Currently I am reading the file into bitarray objects, and am converting subsets of the objects into integers. The process works but is fairly slow and cumbersome. Is there a better way to do this, perhaps with the struct module?

import bitarray

bits = bitarray.bitarray()
with open('/dir/to/any/file.dat','r') as f:
    bits.fromfile(f,2) # read 2 bytes into the bitarray

    ## bits 0:4 represent a field
    field1 = int(bits[0:4].to01(), 2)  # Converts to a string of 0s and 1s, then int()s the string

    ## bits 5:7 represent a field
    field2 = int(bits[4:7].to01(), 2)

    ## bits 8:16 represent a field
    field3 = int(bits[7:16].to01(), 2)

print """All bits: {bits}\n\tfield1: {b1}={field1}\n\tfield2: {b2}={field2}\n\tfield3: {b3}={field3}""".format(
        bits=bits, b1=bits[0:4].to01(), field1=field1, 
        b2=bits[4:7].to01(), field2=field2,
        b3=bits[7:16].to01(), field3=field3)

Outputs:

All bits: bitarray('0000100110000000')
    field1: 0000=0
    field2: 100=4
    field3: 110000000=384
SoreDakeNoKoto

This should work for your specific case:

#bitmasks of fields 1-3, they fit in 2 bytes
FIELD1 = 0b1111000000000000 # first 4 bits
FIELD2 = 0b0000111000000000 # next 3 bits
FIELD3 = 0b0000000111111111 # last 9 bits

def bytes_to_int(num):  #convert bytes object to an int
    res = 0
    num = num[::-1]  # reverse the bytes
    for i in range(len(num)):
        res += num[i] * (256**i)
    return res

def get_fields(f):
    chunk = bytes_to_int(f.read(2))  # read 2 bytes, f1-f3, convert to int
    f1 = (chunk & FIELD1) >> 12  # get each field with its bitmask
    f2 = (chunk & FIELD2) >> 9
    f3 = chunk & FIELD3
    f4 = f.read(f3)  # field4 as a bytes object

    return f1, f2, f3, f4

file = open('file.dat','rb')

#using your sample data
print(get_fields(file))  # returns 0, 4, 384, field4 as a bytes obj

file.close()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fastest way to unpack 32 bits to a 32 byte SIMD vector

From Dev

Fastest way to unpack bits into single precision floats

From Dev

Fastest way to save/load enum[] (byte) to a file

From Dev

Fastest way to "unpack' a pandas dataframe

From Dev

what is the fastest way to count number of set bits in binary file

From Dev

What is the simplest way to extract bits from a byte in a byte array (buffer)?

From Dev

Given 8 bits in a byte, how to find all the possible numbers resulting from flipping of one or more bits in this byte

From Dev

Fastest way to duplicate bits of an integer

From Dev

Start at offset from beginning of file and unpack 4 byte word

From Java

Fastest way to read long[] from file?

From Dev

Fastest way to read a vector<double> from file

From Dev

The fastest way to create a file from a terminal

From Dev

Fastest way to get char* from file in C

From Dev

Fastest way to pack and unpack binary data into list

From Dev

What is the fastest way to sort and unpack a large bytearray?

From Dev

Python: Fastest way to generate unique lists from a range of numbers

From Dev

The fastest and most efficient way to get max number from a list of numbers

From Dev

Fastest way to generate two random numbers from the same range

From Dev

Get bits from byte

From Dev

Is there a cheap way to "mirror" the bits in a byte?

From Java

Fastest way to find lines of a file from another larger file in Bash

From Dev

Fastest Way to Generate Range of Numbers

From Dev

Fastest way to generate random numbers

From Dev

Fastest way to generate magic numbers

From Java

Fastest way to write to file?

From Dev

What is the fastest way to count all set bits?

From Dev

Fastest way to "sort" bit sequence by toggling bits

From

How to unpack 2, 2 and 3 bits out of a byte

From Dev

What is the fastest way in C to extract 8 bits from 8 different bytes?

Related Related

  1. 1

    Fastest way to unpack 32 bits to a 32 byte SIMD vector

  2. 2

    Fastest way to unpack bits into single precision floats

  3. 3

    Fastest way to save/load enum[] (byte) to a file

  4. 4

    Fastest way to "unpack' a pandas dataframe

  5. 5

    what is the fastest way to count number of set bits in binary file

  6. 6

    What is the simplest way to extract bits from a byte in a byte array (buffer)?

  7. 7

    Given 8 bits in a byte, how to find all the possible numbers resulting from flipping of one or more bits in this byte

  8. 8

    Fastest way to duplicate bits of an integer

  9. 9

    Start at offset from beginning of file and unpack 4 byte word

  10. 10

    Fastest way to read long[] from file?

  11. 11

    Fastest way to read a vector<double> from file

  12. 12

    The fastest way to create a file from a terminal

  13. 13

    Fastest way to get char* from file in C

  14. 14

    Fastest way to pack and unpack binary data into list

  15. 15

    What is the fastest way to sort and unpack a large bytearray?

  16. 16

    Python: Fastest way to generate unique lists from a range of numbers

  17. 17

    The fastest and most efficient way to get max number from a list of numbers

  18. 18

    Fastest way to generate two random numbers from the same range

  19. 19

    Get bits from byte

  20. 20

    Is there a cheap way to "mirror" the bits in a byte?

  21. 21

    Fastest way to find lines of a file from another larger file in Bash

  22. 22

    Fastest Way to Generate Range of Numbers

  23. 23

    Fastest way to generate random numbers

  24. 24

    Fastest way to generate magic numbers

  25. 25

    Fastest way to write to file?

  26. 26

    What is the fastest way to count all set bits?

  27. 27

    Fastest way to "sort" bit sequence by toggling bits

  28. 28

    How to unpack 2, 2 and 3 bits out of a byte

  29. 29

    What is the fastest way in C to extract 8 bits from 8 different bytes?

HotTag

Archive