5

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?

7
  • [0x26, 0x9d] isn't actually an array of bytes. It's a list of 2 integers (which are, internally, 4-byte signed values). Do you mean actual bytes or do you mean integers? Please clarify your question. Commented Nov 28, 2008 at 20:27
  • How should I write the desired result as a byte array and not an int array? Commented Nov 28, 2008 at 20:46
  • A sequence of bytes could be a string ('&\x9d') or it could be an actual array (array.array('B',[0x26, 0x9d])). Many people say "array" when they mean list, tuple or some other sequence (like string). Or they could mean "array", which is a separate library. Commented Nov 28, 2008 at 20:50
  • 1
    Sorry. I've been coding Python for two days, so I haven't caught on to its terminology yet. What I want is something that, when written to a file, will give a file with the binary content of 0x269d. Commented Nov 28, 2008 at 21:00
  • See my response. You need to write a string to the file; writing arrays is not supported. Commented Nov 28, 2008 at 21:15

4 Answers 4

6
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'
Sign up to request clarification or add additional context in comments.

1 Comment

int('0010011010011101', 2) alone is probably sufficient for what the OP wants
6

You could do something like this:

>>> s = "0010011010011101"
>>> [int(s[x:x+8], 2) for x in range(0, len(s), 8)]
[38, 157]

1 Comment

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)?
4

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)

1 Comment

The sequence of bytes is what I was looking for. Thanks for the assistance.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.