tags:

views:

382

answers:

3

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?

+5  A: 

You could do something like this:

>>> s = "0010011010011101"
>>> [int(s[x:x+8], 2) for x in range(0, len(s), 8)]
[38, 157]
Greg Hewgill
If I write these integers to a file, will they just use up the byte that is required, or will they take up the full size of an int (4 bytes, I think)?
Chris Lieb
+3  A: 
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'
Martin v. Löwis
int('0010011010011101', 2) alone is probably sufficient for what the OP wants
hop
+4  A: 

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)
S.Lott
The sequence of bytes is what I was looking for. Thanks for the assistance.
Chris Lieb