Thread: How to extract a jpg from a hex file without using EOF

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    How to extract a jpg from a hex file without using EOF

    Hello,

    I am a newbie to programming so please bare with me

    I am trying to extract specific bytes from a hex file... Actually I am trying to create a program that extracts a .jpg from a hex file. So far, I have code that can extract the bytes and out put them to a .jpg until I reach EOF. But, I specifically need to have the program stop once it comes across bytes FF D9 in the hex file without using EOF. It is very important that the program stops once it reaches bytes FF d9 within the hex file for specific data to be extracted. I don't know how I would do this??? I have spend hours on trying to figure this out but I just can't seem to find a solution. This is what I have so far, the code written below is a function to extract a jpg from a hex file:

    Code:
    //extracts the bytes from the inputHexFile and ouputs the bytes to a jpg
    
    void JpegFile ()
    {
    	
         (void)fseek( inputHexFile, 33, SEEK_SET );	//skip over the first 33 bytes
          outputJpegFile = fopen("C:/Documents and Settings/student/Desktop   /Image.jpg", "wb"); //open Image.jpg
    
    	//read the bytes from inputHexFile until the End Of File then output the bytes to outputJpegFile
    	
    	while ((c = fgetc(inputHexFile)) != EOF )  //Needs to be modified to only get bytes until it reaches ff d9 
    	{
    	       fputc(c, outputJpegFile); // outputs bytes to jpeg file
    		
    	} //end while
    	
    	fclose(outputJpegFile);	// close file
    
    }// end JpegFile()
    Thanks in advance for any help

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Look for the byte FF (this is what == is for), then once you've found that check if the next byte is D9.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int c;
    int stopbytes = 0xFFD9;
    int lastbytes = 0;
    while( (c = fgetc( file )) != EOF )
    {
        /* shift over and mask off two bytes */
        lastbytes <<= 8;
        lastbytes &= 0xFFFF;
    
        /* add c */
        lastbytes |= c;
    
        if( lastbytes == stopbytes )
            break;
        else
        {
            ...do something...
        }
    }
    That looks about right.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Just in case it comes up later, the JPEG/JFIF format allows a thumbnail to be stored directly in compliant streams. Simply looking for the "end of image" marker will not be enough in the case of those streams. You wouldn't need to do a lot or parsing, but if you look at the size and type of thumbnail you'll know whether or not you have one and thus whether of not you need to look for a second "end of image" sequence.

    Soma

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    2

    Thank you!

    Quote Originally Posted by quzah View Post
    Code:
    int c;
    int stopbytes = 0xFFD9;
    int lastbytes = 0;
    while( (c = fgetc( file )) != EOF )
    {
        /* shift over and mask off two bytes */
        lastbytes <<= 8;
        lastbytes &= 0xFFFF;
    
        /* add c */
        lastbytes |= c;
    
        if( lastbytes == stopbytes )
            break;
        else
        {
            ...do something...
        }
    }
    That looks about right.

    Quzah.


    Quzah,

    Thank you so much for your help, I was able to modify my code and it appears to work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot extract name from file
    By aadil7 in forum C Programming
    Replies: 13
    Last Post: 12-09-2010, 08:29 PM
  2. How to extract (formatted) text from a PDF file?
    By HeinzB in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2008, 02:46 AM
  3. Extract 1st page from file using C
    By saadahmed42 in forum C Programming
    Replies: 3
    Last Post: 08-18-2008, 02:51 PM
  4. C++ extract only numbers from a .txt file
    By overspray in forum C++ Programming
    Replies: 2
    Last Post: 04-17-2003, 01:27 PM
  5. Need EOF help trying to extract data from .dat file
    By CodeNewB in forum C Programming
    Replies: 3
    Last Post: 09-13-2002, 07:36 PM