How to extract jpg image from hex?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hugo Andrade
    New Member
    • Jan 2011
    • 1

    #1

    How to extract jpg image from hex?

    I want to extract the jpg image from an Hex file. The hex file comes from a serial camera that outputs a hex stream in the serial port. I need to reconstruct the image.

    The output of the camera is attached in the text file.
    the code I'm using reads the hex values and convert them to ASCII and save the file as a .jpg.

    When I try to open the "picture.jp g" with Photo Editor it reads the start FFD8 correctly but it can't display the image.

    Code:
    /**************************************************/
    char hexToAscii(char first, char second)
    {
    	char hex[5], *stop;
    	hex[0] = '0';
    	hex[1] = 'x';
    	hex[2] = first;
    	hex[3] = second;
    	hex[4] = 0;
    	return strtol(hex, &stop, 16);
    }
    /***************************************************/
    int main(int argc, char *argv[])
    {
        ofstream myfile;
        string line, line1;
        myfile.open ("picture.jpg");
    
        ifstream myfile1 ("foto2_HEX.txt");
        if (myfile1.is_open())
        {   i=0;
            while ( myfile1.good() )
            {   getline (myfile1,line);
                for (i=0; i < line.length(); i++)
                {   ab = hexToAscii(line[i], line[i+1]);
                    i++;
                    //cout << ab;
                    myfile << ab;
                }
            }
        }    
        myfile.close();       
        myfile1.close();  
        system("PAUSE");
    
        return EXIT_SUCCESS;
    
    }
    Thanks for your help
    Attached Files
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    What exactly do you mean by the camera "outputs a hex stream in the serial port"? Do you mean that the camera outputs a hex-ASCII stream (the ascii characters '0'-'9' and 'A'-'F') or do you mean it outputs a binary file?

    I suspect the answer is a binary file. If so, then just write the binary data to the file -- get rid of the hexToAscii conversion.

    Comment

    Working...