Thread: Displaying a whole file in hex

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Displaying a whole file in hex

    I want to write a program that would read a whole file and then write another file with its hex values (like a hex editor). I am a little bit confused with everything.

    I have to open the file first. So first question: what is the difference between opening the file in text mode and opening the file in binary mode?. Then, use fread to read chunks of data and store them to an array. Here goes the second question. What should the array type be?

    After that I guess I just do a loop like
    Code:
    for(x=0, x<size, x++)
        fprintf(file, "0x&#37;X ", array[x])
    or something.

    Am I in the right tracks?
    Last edited by samus250; 06-29-2008 at 03:44 PM. Reason: typo

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The syntactic difference in opening is to use a file mode of "rb" versus "r".

    The programmatic difference is that when opening in "text" mode, the access method performs some work on your behalf, in regards to line ending and other control characters (broad description here).

    There's no real point in storing the characters in an array if you will be printing them out a character at a time.

    You'll want to format the hex characters to have a leading zero if the value is less than X'10'.

    With most hex formatters, it is common to break each line after 16 or 32 bytes, and also provide an eye catcher off to the right, and an offset column on left. Each 4-byte word (which is 8 characters when converted to printable hex) is typically separated by a blank or two as well.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    I wrote the program before I saw the code you posted (though I wrote mine how I need it, it creates an unsigned char array with all the hex values, so I can put that code on a program that wants to create the file I scanned)...

    In the hex dump code, the program stops scanning when the 16 byte buffer is not full...
    What I did was check the file size with stat(), but if it fails then scan the whole file and determine the size like that. Then I load the file completely to RAM, using malloc and realloc (starting with a 1MiB buffer), and write the values until I reach the file size stat() gave me.

    Is the way I made it efficient or should I change it to the "write file on the fly with a very small 16 byte buffer

    Thanks.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, efficient might not be a factor.

    I look at it this way.

    1) Is it working?
    2) Does it do the job it was intended to do?
    3) Is it fast enough for who it was intended for?
    4) Is it running now without causing any problems?

    If the answers are yes, you're done. That is, you are done unless you have some other reason for writing it, like honing your efficient-C writing skills.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM