0

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 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

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.