Ke. Ke. - 2 months ago 10
Python Question

Calculate bytes between two hex offsets

In Python, I'm trying to extract some data from a binary file. I know the offsets of my data. They are always the same. For instance, written beneath is the first 4 offsets and the converted offset as a decimal value.


  • Offset1 - 0x00000409 - 1033

  • Offset2 - 0x0000103A - 4154

  • Offset3 - 0x00001C6B - 7275

  • Offset4 - 0x0000289C - 10396



I know that each offset (after the first one), is 3121 decimals apart, so is there a way I can just skip to the next offset? How do I move 3121 decimals to the next offset?

There are 128 offsets that I need to extract. I hope there is a way of dynamically determining the difference (number of bytes) between offsets?

I can then get the same data each time, using 0x100 to extract 256 characters from the offset.

Answer

use file.seek() to skip between locations in a file. In this case, to go to the next location in the file, you would use file.seek(3121, 1) which seeks 3121 bytes ahead relative to the current position.

EDIT: I didn't realize you were changing the file position after opening it, so it should actually be 2685 bytes that you're seeking ahead each time, to account for the 256 you read.

Comments