CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2005
    Posts
    281

    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 .

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Parse Binary for Hex Value

    Quote Originally Posted by Rehorav View Post
    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

  3. #3
    Join Date
    Nov 2005
    Posts
    281

    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 .

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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

  5. #5
    Join Date
    Nov 2005
    Posts
    281

    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
  •  





Click Here to Expand Forum to Full Width

Featured