void hexdump(const void * buf, size_t size)

Simple hexdump function in C

void hexdump(const void * buf, size_t size)
{
  const uchar * cbuf = (const uchar *) buf;
  const ulong BYTES_PER_LINE = 16;
  ulong offset, minioffset;

  for (offset = 0; offset < size; offset += BYTES_PER_LINE)
  {
    // OFFSETXX  xx xx xx xx xx xx xx xx  xx xx . . .
    //     . . . xx xx xx xx xx xx   abcdefghijklmnop
    printf("%08x  ", cbuf + offset);
    for (minioffset = offset;
      minioffset < offset + BYTES_PER_LINE;
      minioffset++)
    {
      if (minioffset - offset == (BYTES_PER_LINE / 2)) {
        printf(" ");
      }

      if (minioffset < size) {
        printf("%02x ", cbuf[minioffset]);
      } else {
        printf("   ");
      }
    }
    printf("  ");

    for (minioffset = offset;
      minioffset < offset + BYTES_PER_LINE;
      minioffset++)
    {
      if (minioffset >= size)
        break;

      if (cbuf[minioffset] < 0x20 ||
        cbuf[minioffset] > 0x7e)
      {
        printf(".");
      } else {
        printf("%c", cbuf[minioffset]);
      }
    }
    printf("\n");
  }
}

How to post code in WordPress

  1. Each line of code must not contain more than 60 characters. Each tab counts as 2 characters.
  2. Save the code in a file with the proper extension, open it gvim and make sure the code is properly color-highlighted. I used Win32 Vim 7.0.
  3. Set the color scheme to a nice one, such as torte.
    Vim command :colorscheme torte
  4. Set the tabstop to a low value.
    Vim command :set tabstop=2
  5. Syntax -> Convert to HTML
  6. Create a new post in the HTML editor, not the rich editor.
  7. Copy and paste everything from <pre> to </pre>.
  8. If you use something like “0x20”, change the “x” to “&#120;” – otherwise, WordPress turns it into a multiply symbol.
    Also, change all backslashes (“\”) to “&#92;”, because otherwise they disappear.
  9. Change the <pre> line to:

    <pre style="color: #cccccc; background: #000000; border: solid 1px #80a0ff; font-size: 1.2em; margin: 10px; padding: 10px">

  10. Post without switching into the rich editor.
Advertisements

4 Responses to void hexdump(const void * buf, size_t size)

  1. […] is given in the previous post.) The output of this program, with the clean (green), dead (blue) and […]

  2. Eli says:

    Wouldn’t it be nice if you could script up as much of that as possible?

    http://pygments.org/ looks like it’s right up your alley.

  3. […] code-posting rant In addition to what I wrote here, you should also replace any apostrophes with: […]

  4. […] What to do? Okay, the reason I haven’t posted in a long while is that it’s incredibly inconvenient. […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: