You have to either convert it to an int and take 8 bits at a time, or chop it into 8 byte long strings and then convert each of them into ints. In Python 3, as PM 2Ring and J.F Sebastian's answers show, the to_bytes()
method of int
allows you to do the first method very efficiently. This is not available in Python 2, so for people stuck with that, the second method may be more efficient. Here is an example:
>>> s = "0110100001101001"
>>> bytes(int(s[i : i + 8], 2) for i in range(0, len(s), 8))
b'hi'
To break this down, the range statement starts at index 0, and gives us indices into the source string, but advances 8 indices at a time. Since s
is 16 characters long, it will give us two indices:
>>> list(range(0, 50, 8))
[0, 8, 16, 24, 32, 40, 48]
>>> list(range(0, len(s), 8))
[0, 8]
(We use list()
here to show the values that will be retrieved from the range iterator in Python 3.)
We can then build on this to break the string apart by taking slices of it that are 8 characters long:
>>> [s[i : i + 8] for i in range(0, len(s), 8)]
['01101000', '01101001']
Then we can convert each of those into integers, base 2:
>>> list(int(s[i : i + 8], 2) for i in range(0, len(s), 8))
[104, 105]
And finally, we wrap the whole thing in bytes()
to get the answer:
>>> bytes(int(s[i : i + 8], 2) for i in range(0, len(s), 8))
b'hi'