[Tutor] Search for hex data in file

Walker Hale IV walker.hale.iv at gmail.com
Mon May 25 18:30:08 CEST 2009


On Mon, May 25, 2009 at 1:32 AM, xbmuncher <xboxmuncher at gmail.com> wrote:
> I've been reading about ways to convert strings and what not to hex and back
> and forth. I'm looking for the fastest and least memory intensive way to
> search through a file for a hex value and gets its byte offset in the file.
> This hex value (that I'm looking for in the file) is of course a hex
> representation of the binary data and its 8 bytes long.
> I figured reading the whole file and converting it to hex from ascii..etc..
> would be overkill, especially if its a large file.
> What do you guys recommend? I want to search the file for certain hex value
> and get the byte offset within the file.
>
> Thanks for reading.

If the input is a text file containing hex strings, then:
* compile a regular expression for a hex string using the re module
* initialize a byte count for previous lines to 0
* read the input file line by line
** search each line for the compiled regular expression
*** for each hit found
**** output a file offset based on the byte count and offset within the line
** add the length of the line to a running total of the number of
bytes in the previous lines

Is that what you are looking for?

-- 
Walker Hale <walker.hale.iv at gmail.com>


More information about the Tutor mailing list