= 2: addr =" /> = 2: addr =" /> = 2: addr ="/>

Python Error TypeError: 'bytes' object cannot be interpreted as an integer when concatenating bytes

235 Views Asked by At

Here's my code:

with open("SomeFile", mode="rb") as file:
    byte = file.read(1)
    count = 0
    previous = []
    while byte != b"": 
        if len(previous) >= 2:
            addr = bytearray(b'')
            addr.append(previous[len(previous) - 2])
            addr.append(previous[len(previous) - 1])
            addr.append(byte)
            addr.append(b'\x00')
            
            addr_int = int.from_bytes(addr, 'little')
            # 0x105df0 == 1072624d
            addr_rel = 1072624 - count;
            if (addr_int == addr_rel):
                print(hex(count))
            
        previous.append(byte)
        if len(previous) > 2:
            previous.pop(0)
        byte = file.read(1)
        count += 1

I followed the method in this question (Python bytes concatenation) but got the error Python Error TypeError: 'bytes' object cannot be interpreted as an integer at line addr.append(previous[len(previous) - 2])

How should I concatenate the bytes from the "previous" array to the "addr" byte sequence?

EDIT:

I found the following method works, but it seems very inefficient as the constructor is called 5 times so I would still like to know the correct way to do it:

            addr = bytearray(b'')
            addr.extend(bytearray(previous[len(previous) - 2]))
            addr.extend(bytearray(previous[len(previous) - 1]))
            addr.extend(bytearray(byte))
            addr.extend(bytearray(b'\x00'))
1

There are 1 best solutions below

0
digitalesch On

I've tried it like this

size = 5

arr = bytes(size)
print(arr)

print(arr + bytes(2))

x = bytearray(arr + bytes(2))
y = bytearray(bytes(2))
print(x)
print(y)
print(x+y)

It would be a simple list adding / concatenation

The terminal result: enter image description here