Python Forum

Full Version: From list of bits to PDF
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!
I am working on a simple program that works for PDF files. So far I’ve been able to read the PDF into a list of bytes with the following code:
    def bstr(n): # n in range 0-255
        return ''.join([str(n >> x & 1) for x in (7,6,5,4,3,2,1,0)])
    def read_binary():
        f = open('source.pdf','rb')
        binlist = [ ]
        while True:
            bin = struct.unpack('B',f.read(1))[0] 
            if not bin:
                break
            strBin = bstr(bin)
            binlist.append(strBin)
        return binlist
After performing some math operations on the list of bit sequences, I would like to get back the PDF file. Is there any way I can reconstruct the PDF file starting from the list of bit sequences?
I would rather do it this way
>>> import io
>>> f = io.BytesIO(b'foo bar baz')
>>> L = [bin(c)[2:] for c in f.read()]
>>> L
['1100110', '1101111', '1101111', '100000', '1100010', '1100001', '1110010', '100000', '1100010', '1100001', '1111010']
>>> b = bytes([int(s, 2) for s in L])
>>> b
b'foo bar baz'