First, I precommuted the string representation for all values ββof 0..255
bytetable = [("00000000"+bin(x)[2:])[-8:] for x in range(256)]
or, if you prefer bits in LSB order for MSB
bytetable = [("00000000"+bin(x)[2:])[-1:-9:-1] for x in range(256)]
then the whole file in binary format can be obtained using
binrep = "".join(bytetable[x] for x in open("file", "rb").read())
source share