chexdump

hexdump program in C language


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 255

void hexDump (char *desc, void *addr, int len) {
    int i;
    unsigned char buffLine[17];
    unsigned char *pc = (unsigned char*)addr;


    if (desc != NULL){

       printf ("%s:\n", desc);

    }


    for (i = 0; i < len; i++) {


        if ((i % 16) == 0) {

            if (i != 0)

                printf ("  %s\n", buffLine);
           // if (buffLine[i]== '\0') break;

            if (pc[i] == 0x00) exit(0);

            // Prints the ADDRESS
            printf ("  %07x ", i);
        }

        // Prints the HEXCODES that represent each chars.
        printf ("%02x", pc[i]);
        if ((i % 2) == 1) 
            printf (" "); 



        if ((pc[i] < 0x20) || (pc[i] > 0x7e)){
            buffLine[i % 16] = '.';
        }

        else{

           buffLine[i % 16] = pc[i];

        }    


        buffLine[(i % 16) + 1] = '\0'; //Clears the next array buffLine


    }



    while ((i % 16) != 0) {
        printf ("   ");
        i++;
    }


    printf ("  %s\n", buffLine);
}

//----------------------------------------------

int main()
    {
            FILE *ptr_file;
            char buff[SIZE];

            ptr_file =fopen("input.txt","r");
            if (!ptr_file){
            printf("returning 1");
                return 1;
            }

            memset(buff, '\0', sizeof( buff) );
            fgets(buff,SIZE, ptr_file);
            hexDump ("buff", &buff, sizeof (buff));

        fclose(ptr_file);

            return 0;
    }
    //*/

Sorry, this I'm new to C, so it's kinda of messy right now, I been deleting parts and making temporary codes, etc... So anyways this works by reading characters from input.txt and outputs it by printing the hexidecimal representation and the Acii representations on the right. For bytes forming non-printable characters, print a ‘.’ character (a dot/period character, i.e. hex value 2E).:

0003540: 0504 0675 6e73 6967 6e65 6420 6368 6172 ...unsigned char
0003550: 0008 0107 0000 0131 0675 6e73 6967 6e65 .......1.unsigne

BUT my program does not print anything beyond the 'new line' in the text. For example: in my input.txt, I have:

This is line 1 so this will be longer than usual
line 2 is this
this is the last line

after reading my program, it only outputs like this:

  0000000 5468 6973 2069 7320 6c69 6e65 2031 2073   This is line 1 s
  0000010 6f20 7468 6973 2077 696c 6c20 6265 206c   o this will be l
  0000020 6f6e 6765 7220 7468 616e 2075 7375 616c   onger than usual
  0000030 0a00 0000 0000 0000 0000 0000 0000 0000   ................

But for some reason it won't print and hex the second, third line....


Solution

  • You should use fread() to read the file, and put that in a loop (until it returns 0), so that it reads the whole file. If you are on Windows also make sure to open the file with "rb" (binary mode), while on Unix it shouldn't matter.

    One last suggestion would be to use isascii() or one of its sister functions, instead of manually checking for "printability". See http://linux.die.net/man/3/isalnum.