Reputation: 3836
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?
Upvotes: 5
Views: 3721
Reputation: 6040
With Python 3 you can convert numbers to bytes natively with number.to_bytes. The byte array can be written directly to a file.
>>> import math,sys
>>> s='0010011010011101'
>>> int(s,2).to_bytes(math.ceil(len(s)/8),sys.byteorder)
b'\x9d&'
>>> with open('/tmp/blah', 'wb') as f:
... f.write(int(s,2).to_bytes(math.ceil(len(s)/8),sys.byteorder))
...
2
>>> quit()
[root@localhost prbs]# od -x /tmp/blah
0000000 269d
0000002
Upvotes: 0
Reputation: 391952
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)
Upvotes: 4
Reputation: 127527
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'
Upvotes: 6
Reputation: 994031
You could do something like this:
>>> s = "0010011010011101"
>>> [int(s[x:x+8], 2) for x in range(0, len(s), 8)]
[38, 157]
Upvotes: 6