0

I have the following bytearray in Python:

bytearray(b'\x02\xcb\x00\n\x02\xcb\x00\n\x02\xcb\x00\n\x02\xcb\x00\n')

I want to convert the hexa values of the bytearray to an array of integer values, by converting \x02\xcb\x00 to an integer 183040 for each '\n'.

It should look like:

[183040, 183040, 183040, 183040]

How can I get it?

I did a list(bytarray) and it just worked for single values not composite ones.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • Does this answer your question? [Convert bytes to int?](https://stackoverflow.com/questions/34009653/convert-bytes-to-int). You just need to split it. And your byte string is malformed with the last slash. – ivvija Nov 11 '22 at 12:35
  • 2
    Using a newline character to separate binary data, seems unwise. – gre_gor Nov 11 '22 at 12:37
  • FYI, you have an extra `\\` at the the end of your bytes string. Python will interpret this as an escape char for the quotation mark. – CreepyRaccoon Nov 11 '22 at 12:40

3 Answers3

1

You might be tempted to split the bytes by the newline character, but the ASCII value of the newline character (10), might show up as part of the integer byte.

The only way this can work, is if the newline character always delimits 3 bytes of the integer.

In this case, you need to iterate over the bytes and take the 3 bytes out of every 4 bytes.

b = b"\x02\xcb\x00\n\x02\xcb\x00\n\x02\xcb\x00\n\n\n\n\n"

numbers = []
for i in range(0, len(b), 4):
    if b[i+3] != 10:
        raise ValueError("Expected a newline character")
    numbers.append(int.from_bytes(b[i:i+3], "big"))
    
print(numbers)

Prints out:

[183040, 183040, 183040, 657930]
gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • I agree that using a newline character to separate binary data is not a good idea but IMHO this is not a valid answer, the request is clearly different. – CreepyRaccoon Nov 11 '22 at 13:36
  • What request is different? – gre_gor Nov 11 '22 at 13:37
  • your solution requires the input to be grouped in integers of 3 bytes, if the question requires to group by `\n` then this might change, right? this is why we agreed that it's not a good idea... – CreepyRaccoon Nov 11 '22 at 14:09
  • I mentioned that constraint. Otherwise it can't work, for reasons I also mentioned. – gre_gor Nov 11 '22 at 14:46
0

You can split the bytearray on newline and then map it to integers

xs = bytearray(b'\x02\xcb\x00\n\x02\xcb\x00\n\x02\xcb\x00\n\x02\xcb\x00\n')

result = []
for line in xs.split(b'\n'):
  result.append(int.from_bytes(line))

result[:-1]

since xs.split(b'\n') return a list ending with bytearry(b'') you should drop the last element.

  • 1
    no way this works, the argument `byteorder` in function `from_bytes()` is required for this purpose – CreepyRaccoon Nov 11 '22 at 12:39
  • I expect `b'\x0a\x0a\x0a\n\x0a\x0a\x0a\n\x0a\x0a\x0a\n\x0a\x0a\x0a\n'` to return `[657930, 657930, 657930, 657930]` not `[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]`. – gre_gor Nov 11 '22 at 12:44
0

Try:

b = bytearray(b'\x02\xcb\x00\n\x02\xcb\x00\n\x02\xcb\x00\n\x02\xcb\x00\n')
int_list = [int.from_bytes(n, "big", signed=False) for n in b.split(b'\n')[:-1]]
print(int_list)

Output:

[183040, 183040, 183040, 183040]

Just be careful about the endianess ["big", "little"] and the sign.

CreepyRaccoon
  • 826
  • 1
  • 9
  • 19