Chris Lieb
Chris Lieb

Reputation: 3836

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?

Upvotes: 5

Views: 3721

Answers (4)

shrewmouse
shrewmouse

Reputation: 6040

Python 3 and number.to_bytes

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()

file contents

[root@localhost prbs]# od -x /tmp/blah
0000000 269d
0000002

Upvotes: 0

S.Lott
S.Lott

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

Martin v. Löwis
Martin v. Löwis

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

Greg Hewgill
Greg Hewgill

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

Related Questions