Converting a string of 1's and 0's to a byte array
I have a string with a length that is a multiple of 8 that contains only 0's and 1's. I want to convert the string into a byte array suitable for writing to a file. For instance, if I have the string "0010011010011101", I want to get the byte array [0x26, 0x9d], which, when written to file, will give 0x269d as the binary (raw) contents.
How can I do this in Python?
py> data = "0010011010011101" py> data = [data[8*i:8*(i+1)] for i in range(len(data)/8)] py> data ['00100110', '10011101'] py> data = [int(i, 2) for i in data] py> data [38, 157] py> data = ''.join(chr(i) for i in data) py> data '&\x9d'
You could do something like this:
>>> s = "0010011010011101" >>> [int(s[x:x+8], 2) for x in range(0, len(s), 8)] [38, 157]
Your question shows a sequence of integers, but says "array of bytes" and also says "when written to file, will give 0x269d as the binary (raw) contents". These are three very different things. I think you've over-specified. From your various comments it looks like you only want the file output, and the other descriptions were not what you wanted.
If you want a sequence of integers, look at Greg Hewgill's answer.
If you want a sequence of bytes (as in a string) -- which can be written to a file -- look at Martin v. Löwis answer.
If you wanted an array of bytes, you have to do this.
import array intList= [int(s[x:x+8], 2) for x in range(0, len(s), 8)] byteArray= array.array('B', intList)