Ad

Python: Trying To Split Instream Into Bit Sized Chunks

- 1 answer

Currently I'm reading an instream and splicing it into 3 byte chunks:

instream_chunks = [instream[i:i+3]for i in range (0, len(instream), 3)]

What I'm failing to do is to split this instream into 22 bit sized chunks. Is there a way to do that in Python?

Edit: The instream is created (for test purposes) like this:

instream = open('C:/xxx/test.txt', 'rb+')

And this instream is then being used in this function

def write(self, instream: typ.BinaryIO):

Which starts with what I described above.

Ad

Answer

Assuming you have enough memory, you can convert the instream to a list of bits and then slice it however you want.

def access_bit(data, num):
    base = int(num/8)
    shift = num % 8
    return (data[base] & (1<<shift)) >> shift

def to_bits(instream):
    ba = bytearray(instream.read())
    return [access_bit(ba,i) for i in range(len(ba)*8)]

>>>to_bits(open('test.txt','rb'))

[0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0]

Otherwise you'll have to read smaller chunks in multiple of size you want, and then use the above method on each. For example, you read 22*4 = 88 bits, or 11 bytes, and then call to_bits on that, split the resulting array into 4 22bit chunks, and repeat.

Ad
source: stackoverflow.com
Ad