open binary file and search hex string

  • Thread starter Thread starter Nikos
  • Start date Start date
N

Nikos

Hi... I would like to search for a hex string (for example: "E903") inside a
binary file...
Although I open the file correctly, how do I search hex values?
Thanks in advance!
Nikos
 
Nikos,

Are you looking for the character string "E903" or for the two-byte
value 59651? Either way, once you know what bytes you are looking for, you
need to open the file with a FileStream, and cycle through all of the bytes,
looking one-by-one for a match.

Hope this helps.
 
Thanks for the reply... I am actually looking for character string "E903"...
I am stuck with the search routine. How to search hex values?
Should I load all file contents to an array and search? Or, is it possible
to just read the file byte after byte and examine the offset of the search
string?
Nikos



Nicholas Paldino said:
Nikos,

Are you looking for the character string "E903" or for the two-byte
value 59651? Either way, once you know what bytes you are looking for,
you need to open the file with a FileStream, and cycle through all of the
bytes, looking one-by-one for a match.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nikos said:
Hi... I would like to search for a hex string (for example: "E903")
inside a binary file...
Although I open the file correctly, how do I search hex values?
Thanks in advance!
Nikos
 
Nikos,

I would just read the file byte by byte, keeping a buffer of the last n
bytes read, where n is the length of the bytes you want to search for. If
you are looking for that character string, then you should use the GetBytes
method on the AsciiEncoder class in the System.Text namespace to get the
bytes that the string actually is comprised of.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nikos said:
Thanks for the reply... I am actually looking for character string
"E903"...
I am stuck with the search routine. How to search hex values?
Should I load all file contents to an array and search? Or, is it
possible to just read the file byte after byte and examine the offset of
the search string?
Nikos



Nicholas Paldino said:
Nikos,

Are you looking for the character string "E903" or for the two-byte
value 59651? Either way, once you know what bytes you are looking for,
you need to open the file with a FileStream, and cycle through all of the
bytes, looking one-by-one for a match.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nikos said:
Hi... I would like to search for a hex string (for example: "E903")
inside a binary file...
Although I open the file correctly, how do I search hex values?
Thanks in advance!
Nikos
 
Back
Top