-
May 15th, 2010, 06:31 PM
#1
Parse Binary for Hex Value
Hey everyone, I'm trying to parse a binary file for a certain file, and I found a way but its a inefficient. Basically, what I'm trying to do is find a value, and then write over it. But right now I'm just finding the value. Here is the code, hopefully someone can give me a suggestion to make it so I can just search for the entire value instead of byte by byte.
Code:
int main ()
{
FILE *File1;
if ( ( File1 = fopen ( "App.exe", "rb" ) ) == NULL )
{
MessageBox ( NULL, "Can't Open First File", "Messup", NULL );
}
unsigned char Char1;
int Loc = 0;
while ( !feof ( File1 ) )
{
Char1 = fgetc ( File1 );
if ( Char1 == 0xDD )
{
Loc = 1;
}
else if ( Char1 == 0xCC && Loc == 1 )
{
Loc = 2;
}
else if ( Char1 == 0xBB && Loc == 2 )
{
Loc = 3;
}
else if ( Char1 == 0xAA && Loc == 3 )
{
MessageBox ( NULL, "Found", "Found", NULL );
}
else
{
Loc = 0;
}
}
}
As you can tell, I'm looking for 0xAABBCCDD (endian ordering of course).
As I said, its very inefficient. If someone could tell me how to find the whole value at once, that would be amazing .
And can someone tell me how to write over that value once I find it?
And if you're wondering why I'm doing this, its an experiment I'm trying with finding values with files. Kind of a boring Saturday so why not .
-
May 16th, 2010, 06:58 AM
#2
Re: Parse Binary for Hex Value
Originally Posted by Rehorav
As I said, its very inefficient. If someone could tell me how to find the whole value at once, that would be amazing .
Just use fread.
And can someone tell me how to write over that value once I find it?
Just use fwrite.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
May 16th, 2010, 12:25 PM
#3
Re: Parse Binary for Hex Value
Ah alright thank you! I've been googling about looking for how to search for an exact value with fread, but I couldn't find anything to help me .
-
May 17th, 2010, 03:15 AM
#4
Re: Parse Binary for Hex Value
Hi,
You might also want to read the whole file, or blocks of it, into a memory buffer, then search through that. You will be able to test for 4 bytes at a time
Alan
-
May 17th, 2010, 11:14 AM
#5
Re: Parse Binary for Hex Value
I have been looking around on how I would read the file then search for the value, I'm still stumped . I get how to open a file, but I don't get how to look for that exact value.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|