So I have a stream of bits in python that looks like that:
bitStream = "001011000011011111000011100111000101001111100011001"
Now this stream is dynamic, meaning that it changes depending on the input received, now I want to write this to a file in python, I’m currently doing that:
f = open("file.txt", "rb+") s = file.read() # stream bitStream = "001011000011011111000011100111000101001111100011001" byteStream = int(bitStream,2).to_bytes(len(bitStream)//8, 'little') f.close() #close handle
However that works but the thing is that the bit stream can be a non-8bits aligned string and in this case it writes to file
n-1 bytes. Now normally I would align the file to be divisible by 8 but in this case I really cannot add bits otherwise, when I would give again this file to my program it will misinterpret the alignment bits as something other than expected.
Would you guys have any idea?
Thanks in advance
Source: Python Questions