JPG Image extraction from hex file

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 hex output of the camera is something like this:

FFD8FFFE002446007C0C0000000000000000000000000000007800A000220032120B510451040000FFDB008400090606070605090707070A09090A0D150E0D0C0C0D1A13140F151F1B20201E1B1E1D2226312922242E251D1E2B3A2B2E323437373721293C403C35403136373501090A0A0D0B0D190E0E19C2267437292CB6B24704DE4C8CA42C9B73B4FAE2B9AB5F0CDED887104F0CCCC725EA2F4EB8A883D4A8EE6F782B588EF7458D0BE5D0608AD2BDD5522CA47877FD0526B5135A9FFD2E56E82DF6BF1DB28CC76BFBC93D0B7615B10EA0B.............
..........
.............08F2E5888503019471F950644E4C3320284106ABB44636F94E476A091B3411DD426391721BAFB57397968F693189BA7F09F51417165573B4649AAAEE8E79193ED4163E3573C0CE3D1ABA4F0CEB13683E7C820591A450A016C01EF59CF6B0EDF9C8E9549C8CF51409267FFFFFFFD9

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.jpg" with Photo Editor it reads the start FFD8 correctly but it can't display the image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**************************************************/
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 photo;
    string line, line1;
    photo.open ("picture.jpg");

    ifstream photo_hex ("foto2_HEX.txt");
    if (photo_hex.is_open())
    {   i=0;
        while ( photo_hex.good() )
        {   getline (photo_hex,line);
            for (i=0; i < line.length(); i++)
            {   ab = hexToAscii(line[i], line[i+1]);
                i++;
                //cout << ab;
                photo << ab;
            }
        }
    }    
    photo.close();       
    photo_hex.close();  

    return EXIT_SUCCESS;

}


Thanks for your advice
Topic archived. No new replies allowed.