1
\$\begingroup\$

I have a question which I was unable to solve but it seemed trivial. I am getting a stream of characters from a processor over UART and this stream represents a JPEG file.

I captured this stream, as file.txt and can open it in (say) notepad and see FFD8...lots of data...FFD9. FFD8 and FFD9 are JPEG start of image and end of image codes and the data in between is presumably the jpeg image. I want to view this image, but the problem is that this data represents HEX characters (FFD8 for instance) but being a text file it is actually just ASCII and hence I can not open it as a JPEG.

Is there a program or a utility which will let me convert this file which contains "ASCII HEX" to true binary so I can view it as a jpeg file?

Thanks!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ The standard C function fir this conversion is atoi(). When you google it you'll find others, optimized for integer length and other stuff. \$\endgroup\$ Apr 3, 2015 at 2:53
  • \$\begingroup\$ What microprocessor or microcontroller? What compiler? \$\endgroup\$ Apr 3, 2015 at 3:02
  • \$\begingroup\$ you could read the characters one at a time, and convert to the corresponding value using a lookup table (or a switch case etc) and adding them into a software buffer representing the buffer/stream for that frame, and memset it all back to 0 between each streamed frame? \$\endgroup\$
    – KyranF
    Apr 3, 2015 at 3:03
  • \$\begingroup\$ Microcontroller is STM32F4 with the GCC compiler. \$\endgroup\$
    – IgorEE
    Apr 3, 2015 at 3:50

2 Answers 2

2
\$\begingroup\$

First you will need s short subroutine to convert each hex 4-bit nibble to binary, and then save off the nibbles as 8-bit binary values.

unsigned char hextobin (unsigned char digit)
{
    unsigned char value;

    if ((toupper(digit) >= 'A')&&(toupper(digit) <= 'F'))
    {
        value = (digit - 'A') + 10;
    }
    else if ((digit >= '0') && (digit <= '9'))
    {
         value = digit - '0';
    }
    else
    {
         value = 255;    // error - not handling this below
    }
    return value;
}

Then for every two hex characters, call the above routine twice; for example if you are inputting them in a loop, somethig like this:

    unsigned char nibble1, nibble2, byte;

// assume inside a loop getting a characters from UART

{

    nibble1 = gethexchar();    // ssume gets one ASCII char 0-9 or A-F
    nibble2 = gethexchar();

    byte = 256 * hextobin(nibble1) + hextobin(nibble2);

    // can save byte off to buffer or write to file
}
\$\endgroup\$
2
\$\begingroup\$

I found an easy way to do this, although tcrosley's answer above is also correct.

My way was to change the code on the microprocessor to output the data with a %c format specifier instead of the %x format specifier originally used.

Then I capture the data using RealTerm to a file.

Then I open the file with a HEX editor (hexplorer) to verify it starts with FFD8 and ends with FFD9 (I usually have to delete some characters from the end of the file, this is added due to how the firmware sends the file).

Then I rename the file to jpg extension and it can be opened with any image viewing software.

Thanks for the help everyone!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.