0

I have an array of int values range 0 to 1. ie [0,1,1,0,0,1,0,0,1...]. How can I use bytearray to write this into a file whereby each byte contains 8 elements in the array (8 bits). | and byte heading are only for example:

Output:

| byte 1 | byte 2 | etc...
|01100100|1...    |

011001001...
llamaro25
  • 642
  • 1
  • 7
  • 22

3 Answers3

1

Here there is an over engineered solution that works only when the list has a number of elements multiple of 8

l = [0,1,1,0,1,1,0,0,
     1,0,1,0,1,1,1,0,
     0,1,0,1,0,1,0,1,
     0,0,1,1,0,0,0,0]

for n in range(len(l)//8):
    print('| byte ', n+1, ' ', sep='', end='')

print('|')

for byte in zip(*[iter(l)]*8):
    print('|', *byte, sep='', end='')

print('|')

out:

| byte 1 | byte 2 | byte 3 | byte 4 |
|01101100|10101110|01010101|00110000|
Hoxha Alban
  • 1,042
  • 1
  • 8
  • 12
0

Since I must post my own code, here we go.

arr = [0,1,1,0,1,1,1,0,1] 

bytearr = bytearray()
x = ''
for i in range(len(arr)):
    if i > 0 and i % 8 == 0:
        bytearr.append(int(x,2))
        x = str(arr[i])
    else:
        x += str(arr[i])

# handle remaining bits
if x != '':
    bytearr.append(int(x,2))
llamaro25
  • 642
  • 1
  • 7
  • 22
-1

You should post your own code, not ask someone to write it for you, and you were mean to me. But it sounds like a useful question for other people to look up, so here you go:

(a week late, just in case this was homework)

# helper function with some basic math
def number(digits, base):
    value = 0
    for digit in digits:
        value *= base
        value += int(digit)
    return value

if 826 != number('826', 10):
    raise ValueError

if 64+16+2+1 != number([0, 1, 0, 1, 0, 0, 1, 1], 2):
    raise ValueError

# helper function to break the data up into the expected chunks
# https://stackoverflow.com/a/312464/1766544
def chunks(arbitrary_list, chunk_size):
    for i in range(0, len(arbitrary_list), chunk_size):
        yield arbitrary_list[i:i + chunk_size]

data = [
    0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 1,
    0, 0, 0, 0, 0, 0, 1, 0,
    0, 0, 0, 0, 0, 1, 0, 0,
    0, 0, 0, 0, 1, 0, 0, 0,
    0, 0, 0, 1, 0, 0, 0, 0,
    0, 0, 1, 0, 0, 0, 0, 0,
    0, 1, 0, 0, 0, 0, 0, 0,
    1, 0, 0, 0, 0, 0, 0, 0,
]

# the bytearray constructor needs an iterable
# the iterable needs to yield the number
# the number needs 8 bits at a time
binary_data = bytearray((number(bits, base=2) for bits in chunks(data, 8)))

# write the bytearray to a file is too easy
# there's already a function that writes stuff to a file
#with open ('filename.data', 'wb') as f:
    #f.write(binary_data)

# but easier to demonstrate that we have the right values like so:
for n in binary_data:
    print(n)

output:

0
1
2
4
8
16
32
64
128

Kenny Ostrom
  • 5,639
  • 2
  • 21
  • 30
  • mean to you? go cry to mummy. take some criticism that your links weren't useful and move on. maybe don't stay on the site if your intention isn't to help – llamaro25 Jun 11 '20 at 13:18
  • also fyi number isn't a function. ```name 'number' is not defined``` – llamaro25 Jun 11 '20 at 13:39