I have a dilemma where I have a string of binary code that is all one line. Like this 0110000101110000011100000110110001100101
(or apple
), but I need a way to read every 8 characters to form the full code for the letter so I can convert it.
For some background on how I store the binary, I store it in a dict with the keys as the letters and binary as a string: binary = {"a": "01100001"}
, later I swap the keys and values. I can't find a way to read the string in 8 characters chunks like so 01100001 = a
or 01100101 = e
, but it has to happen all at once in a for a loop.
def binary_string(bin):
res = dict((v, k) for k, v in binary.items())
data = []
for bit in bin:
if bit in res:
data.append(res[bin])
else:
print("Couldn't find " + bit + "'s letter")
load = str(data).strip("[]")
return load
I am looking for a way to say: for every 8 characters in this string: append the letter equivalent to a list, and from there I have no problem creating the output to my desire.