none
Code for Converting Hexadecimal File to ASCII File

    Question

  • Dear Win techies,

    I just wanted to know how can we convert a Hexadecimal File to ASCII File in C?
    Its is basically a Hex dump file (consists of hexadecimal content) and I wanted to scan it and convert it into an ASCII Code. And that Hex file cannot to opened in a notepad or wordpad, its related to a project and can be viewed only in Visual Stduio IDE.
    I hope you guys got some idea regarding the objective.
    Can anybody give me some clue..?

    Thanks in advance.

    Regards,
    Aditya.
    • Edited by aditz Tuesday, February 23, 2010 8:14 AM
    Tuesday, February 23, 2010 4:24 AM

Answers

  • Instead of rolling your own code, you may want to use a 3rd party hex viewer, like Cygnus Hex Editor (available for free, too.)

    Giovanni

    • Marked as answer by Nancy Shao Monday, March 01, 2010 6:49 AM
    Tuesday, February 23, 2010 9:58 AM
  • This is not something that should take an enormous amount of your development cycle. Understanding the difference between a binary and text file is more important, I think.

    If Notepad cannot open the file...its either binary, or too large. If you cannot use the DOS (Sorry, I mean CMD) utility type on it...its binary.

    dump file is a fairly generic term, so it is not at all clear what you mean by the terms hex file and hex dump. In my vernacular a hex dump is a hex file, and a hex file is a text file. There is no such thing as a hexadecimal representation that is NOT text. Computers store in binary only, and the terms text and ASCII are the same thing, too.

    C is pretty good at doing this type of thing, although there are other ways that are likely more preferred, this snippet shows how to use sprintf to convert it for you. I made a BAD example by making my "binary" data actually text, but it really should not matter, because the output is that hex text representation you are looking for. The spaces are there for no particular reason; in a file, you would not have them. A hex dump would just be those 2 character hexadecimal values in an uninterrupted "stream". Any software that displayed it would have to separate the values, and layout the display.


    HOWEVER...You already have a "hex file viewer" in VS. Its called the Binary Editor, oddly enough. You can choose "open File", find your "hex dump", and rather than pressing the Open button....drop the list, and choose "Open With...", select the Binary Editor, and there is your BINARY file, displayed as hexadecimal, and also as characters. You can also EDIT that file, and save it.

    Here is a snippet that shows binary to hex conversion, using sprintf...

    	byte text[] = {'T', 'E', 'X', 'T', 0};
    	char textbuff[12] = {0};
    	sprintf(textbuff,
    			"%02x %02x %02x %02x",
    			(int)text[0],
    			(int)text[1],
    			(int)text[2],
    			(int)text[3]);
    

    • Marked as answer by Nancy Shao Monday, March 01, 2010 6:49 AM
    Tuesday, February 23, 2010 10:39 PM

All replies

  • I can only guess that by "hexadecimal", you mean a binary file.  Well, even ASCII (text) files are binary.  Your requirement doesn't tell much, if any at all.  Please be more specific.  What type of file do you have?  How does it look like when open in Notepad?  Do you know how this file is being created?
    MCP
    Tuesday, February 23, 2010 5:14 AM
  • Instead of rolling your own code, you may want to use a 3rd party hex viewer, like Cygnus Hex Editor (available for free, too.)

    Giovanni

    • Marked as answer by Nancy Shao Monday, March 01, 2010 6:49 AM
    Tuesday, February 23, 2010 9:58 AM
  • This is not something that should take an enormous amount of your development cycle. Understanding the difference between a binary and text file is more important, I think.

    If Notepad cannot open the file...its either binary, or too large. If you cannot use the DOS (Sorry, I mean CMD) utility type on it...its binary.

    dump file is a fairly generic term, so it is not at all clear what you mean by the terms hex file and hex dump. In my vernacular a hex dump is a hex file, and a hex file is a text file. There is no such thing as a hexadecimal representation that is NOT text. Computers store in binary only, and the terms text and ASCII are the same thing, too.

    C is pretty good at doing this type of thing, although there are other ways that are likely more preferred, this snippet shows how to use sprintf to convert it for you. I made a BAD example by making my "binary" data actually text, but it really should not matter, because the output is that hex text representation you are looking for. The spaces are there for no particular reason; in a file, you would not have them. A hex dump would just be those 2 character hexadecimal values in an uninterrupted "stream". Any software that displayed it would have to separate the values, and layout the display.


    HOWEVER...You already have a "hex file viewer" in VS. Its called the Binary Editor, oddly enough. You can choose "open File", find your "hex dump", and rather than pressing the Open button....drop the list, and choose "Open With...", select the Binary Editor, and there is your BINARY file, displayed as hexadecimal, and also as characters. You can also EDIT that file, and save it.

    Here is a snippet that shows binary to hex conversion, using sprintf...

    	byte text[] = {'T', 'E', 'X', 'T', 0};
    	char textbuff[12] = {0};
    	sprintf(textbuff,
    			"%02x %02x %02x %02x",
    			(int)text[0],
    			(int)text[1],
    			(int)text[2],
    			(int)text[3]);
    

    • Marked as answer by Nancy Shao Monday, March 01, 2010 6:49 AM
    Tuesday, February 23, 2010 10:39 PM