Pattern Search in Binary FIle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kenone
    New Member
    • Jul 2008
    • 3

    Pattern Search in Binary FIle

    I have loaded a large binary file into memory and now I want to search for 10101. I was using file.get to return the next hex number and see if it was equal to 0x15. This is not correct as part of my seach pattern (10101) may straggle over two hex numbers.

    Does anyone know of a way to find the pattern 10101 in a binary file loaded into memory?

    Any help is appreciated.
    Last edited by kenone; Jul 25 '08, 12:43 PM. Reason: Missed out the word file
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    Hi,
    One thing that might help is a function that finds a bit at a particular index in a char array. It would take a char array and an index as arguments and return a boolean value for the bit at that index. Indices 0-7 would be bits in the first character, 8-15 would be in the second, and so on. What the function would do is:
    1: calculate the char to look in (with the / operator),
    2: calculate the bit in that char (with the % operator),
    3: return the value there.

    In case you don't know get a bit at a certain position out of a char already, here's how:
    1: Make a char that only has a bit at that position, by starting with the value 1 and bit shifting it into that position, like this:
    1 << thatPosition
    OK, I lied, that's not a char, it's an int, but it works just as well.
    2: Compare that value to the char with the bitwise & operator, like this:
    thatValue & theChar
    Here's an example:
    00000100 &
    10101100
    See, that would only evaluate to true if the char had a true bit in that position.

    Hope this helps.

    Comment

    Working...