Skip to content

Instantly share code, notes, and snippets.

@fuzzamos
Forked from richinseattle/hexdump.c
Created May 25, 2019 11:56
Show Gist options
  • Save fuzzamos/ff33597a18f7ec28f2153ab80733ceb3 to your computer and use it in GitHub Desktop.
Save fuzzamos/ff33597a18f7ec28f2153ab80733ceb3 to your computer and use it in GitHub Desktop.

Revisions

  1. @richinseattle richinseattle revised this gist May 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hexdump.c
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    #include <ctype.h>

    #ifndef HEXDUMP_COLS
    #define HEXDUMP_COLS 32
    #define HEXDUMP_COLS 16
    #endif

    void hexdump(void *mem, unsigned int len)
  2. @richinseattle richinseattle revised this gist May 8, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hexdump.c
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    #include <ctype.h>

    #ifndef HEXDUMP_COLS
    #define HEXDUMP_COLS 8
    #define HEXDUMP_COLS 32
    #endif

    void hexdump(void *mem, unsigned int len)
  3. @richinseattle richinseattle created this gist May 8, 2017.
    60 changes: 60 additions & 0 deletions hexdump.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #include <stdio.h>
    #include <ctype.h>

    #ifndef HEXDUMP_COLS
    #define HEXDUMP_COLS 8
    #endif

    void hexdump(void *mem, unsigned int len)
    {
    unsigned int i, j;

    for(i = 0; i < len + ((len % HEXDUMP_COLS) ? (HEXDUMP_COLS - len % HEXDUMP_COLS) : 0); i++)
    {
    /* print offset */
    if(i % HEXDUMP_COLS == 0)
    {
    printf("0x%06x: ", i);
    }

    /* print hex data */
    if(i < len)
    {
    printf("%02x ", 0xFF & ((char*)mem)[i]);
    }
    else /* end of block, just aligning for ASCII dump */
    {
    printf(" ");
    }

    /* print ASCII dump */
    if(i % HEXDUMP_COLS == (HEXDUMP_COLS - 1))
    {
    for(j = i - (HEXDUMP_COLS - 1); j <= i; j++)
    {
    if(j >= len) /* end of block, not really printing */
    {
    putchar(' ');
    }
    else if(isprint(((char*)mem)[j])) /* printable char */
    {
    putchar(0xFF & ((char*)mem)[j]);
    }
    else /* other char */
    {
    putchar('.');
    }
    }
    putchar('\n');
    }
    }
    }

    #ifdef HEXDUMP_TEST
    int main(int argc, char *argv[])
    {
    hexdump(argv[0], 20);

    return 0;
    }
    #endif