Everything in code when well there is a small problem on display output:

when I run the file this is the output i got

69 6E 74 20 6D 61 69 6E 20 28 29 20 7B 7D 0A

my teacher have

0000 : 69 6E 74 20 6D 61 69 6E 20 28 29 20 7B 7D 0A

i missing the 0000: in front! I tired so way but no go at all.

I hope someone can help me out here.

#include <stdio.h>
#include <stdlib.h>
#define HEX_OFFSET    1
#define ASCII_OFFSET 51
#define NUM_CHARS    16

void   hexdump    (char* prog_name, char * filename);

/* Clear the display line.  */
void   clear_line (char *line, int size);

/* Put a character (in hex format
             * into the display line. */
char * hex   (char *position, int c);

/* Put a character (in ASCII format
             * into the display line. */
char * ascii (char *position, int c);

main(int argc, char * argv[])
    char *prog_name="hexdump";

    if (argc != 2)
        printf("\n\t%s syntax:\n\n", argv[0]);
        printf("\t\t%s filename\n\n", argv[0]);

    hexdump( argv[0], argv[1]);

void hexdump(char* prog_name, char * filename)
    int c=' ';                    /* Character read from the file */

    char * hex_offset;     /* Position of the next character
                                                 * in Hex     */

    char * ascii_offset;      /* Position of the next character
                                                       * in ASCII.      */

    FILE *ptr;                       /* Pointer to the file.   */

    char line[81];        /* O/P line.      */

    /* Open the file    */
    ptr = fopen(filename,"r");
    if ( ferror(ptr) )
        printf("\n\t%s: Unable to open %s\n\n", prog_name, filename);

    printf("\n\tHex dump of %s\n\n", filename);

    while (c != EOF )
        clear_line(line, sizeof line);
        hex_offset   = line+HEX_OFFSET;
        ascii_offset = line+ASCII_OFFSET;

        while ( ascii_offset < line+ASCII_OFFSET+NUM_CHARS
                &&(c = fgetc(ptr)) != EOF  )
            /* Build the hex part of
             * the line.      */
            hex_offset = hex(hex_offset, c);

            /* Build the Ascii part of
             * the line.      */
            ascii_offset = ascii(ascii_offset, c);

        printf("%s\n", line);


void clear_line(char *line, int size)
    int count;

    for  (count=0; count < size; line[count]=' ', count++);

char * ascii(char *position, int c)
    /* If the character is NOT printable
     * replace it with a '.'  */
    if (!isprint(c)) c='.';

    sprintf(position, "%c", c);    /* Put the character to the line
                                    * so it can be displayed later */

    /* Return the position of the next
     * ASCII character.   */

char * hex(char *position, int c)
    int offset=3;

    sprintf(position, "%02X ", (unsigned char) c);

    *(position+offset)=' ';   /* Remove the '/0' created by 'sprint'  */

    return (position+offset);

Recommended Answers

All 8 Replies

It is quite simple: first, you need an unsigned int variable as a counter, which should be initialized to zero. Alter your printf() statement to print this counter as a four-digit hex value followed by a a semi-colon and a space, followed by the string you are now printing. Finally, at the end of the printing loop, add NUM_CHARS to the counter.

i dont understand?

can you show me?

Which part are you having trouble with?

Here is what i put it in

it work but it only show 0000:

clear_line(line, sizeof line);
        int lines = 0;
        printf("%04x: ", lines);
        lines += 16;
        hex_offset   = line+HEX_OFFSET;
        ascii_offset = line+ASCII_OFFSET;

OK, first thing is, in C you can only declare variables at the beginning of the function, so I'm not sure why it is letting you put it there. The fact that it is inside the loop, however, means it is getting re-initialized each iteration, so it will always be zero.

Also, I assume that it is still printing out the rest of the line, correct?


and so on

did i put in wrong place?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.