Off-the-Shelf C++ Hex Dump Code
Asked Answered
S

7

16

I work a lot with network and serial communications software, so it is often necessary for me to have code to display or log hex dumps of data packets.

Every time I do this, I write yet another hex-dump routine from scratch. I'm about to do so again, but figured I'd ask here: Is there any good free hex dump code for C++ out there somewhere?

Features I'd like:

  • N bytes per line (where N is somehow configurable)
  • optional ASCII/UTF8 dump alongside the hex
  • configurable indentation, per-line prefixes, per-line suffixes, etc.
  • minimal dependencies (ideally, I'd like the code to all be in a header file, or be a snippet I can just paste in)

Edit: Clarification: I am looking for code that I can easily drop in to my own programs to write to stderr, stdout, log files, or other such output streams. I'm not looking for a command-line hex dump utility.

Sakhuja answered 27/8, 2008 at 0:18 Comment(0)
B
5

The unix tool xxd is distributed as part of vim, and according to http://www.vmunix.com/vim/util.html#xxd, the source for xxd is ftp://ftp.uni-erlangen.de:21/pub/utilities/etc/xxd-1.10.tar.gz. It was written in C and is about 721 lines. The only licensing information given for it is this:

* Distribute freely and credit me,
* make money and share with me,
* lose money and don't ask me.

The unix tool hexdump is available from http://gd.tuwien.ac.at/softeng/Aegis/hexdump.html. It was written in C and can be compiled from source. It's quite a bit bigger than xxd, and is distributed under the GPL.

Breakup answered 27/8, 2008 at 2:42 Comment(0)
B
49

I often use this little snippet I've written long time ago. It's short and easy to add anywhere when debugging etc...

#include <ctype.h>
#include <stdio.h>

void hexdump(void *ptr, int buflen) {
  unsigned char *buf = (unsigned char*)ptr;
  int i, j;
  for (i=0; i<buflen; i+=16) {
    printf("%06x: ", i);
    for (j=0; j<16; j++) 
      if (i+j < buflen)
        printf("%02x ", buf[i+j]);
      else
        printf("   ");
    printf(" ");
    for (j=0; j<16; j++) 
      if (i+j < buflen)
        printf("%c", isprint(buf[i+j]) ? buf[i+j] : '.');
    printf("\n");
  }
}
Bullbat answered 27/8, 2008 at 10:35 Comment(2)
perfect! this rocks. i am using a socket reader and i called the function with "hexdump(buffer, bytecount);" and it works like a charm! thank you!Pneumonoultramicroscopicsilicovolcanoconiosis
printf("%02x ", (unsigned)(buf[i+j]));Leavetaking
B
5

The unix tool xxd is distributed as part of vim, and according to http://www.vmunix.com/vim/util.html#xxd, the source for xxd is ftp://ftp.uni-erlangen.de:21/pub/utilities/etc/xxd-1.10.tar.gz. It was written in C and is about 721 lines. The only licensing information given for it is this:

* Distribute freely and credit me,
* make money and share with me,
* lose money and don't ask me.

The unix tool hexdump is available from http://gd.tuwien.ac.at/softeng/Aegis/hexdump.html. It was written in C and can be compiled from source. It's quite a bit bigger than xxd, and is distributed under the GPL.

Breakup answered 27/8, 2008 at 2:42 Comment(0)
E
2

Just in case someone finds it useful...

I've found single function implementation for ascii/hex dumper in this answer.

A C++ version based on the same answer with ANSI terminal colours can be found here.

More lightweight than xxd.

Expedition answered 27/8, 2008 at 0:18 Comment(0)
K
0

Could you write your own dissector for Wireshark?

Edit: written before the precision in the question

Ketcham answered 27/8, 2008 at 0:35 Comment(0)
B
0

I have seen PSPad used as a hex editor, but I usually do the same thing you do. I'm surprised there's not an "instant answer" for this question. It's a very common need.

Bikales answered 27/8, 2008 at 0:58 Comment(0)
A
0

I used this in one of my internal tools at work.

Arterialize answered 27/8, 2008 at 19:40 Comment(0)
S
-1

xxd is the 'standard' hex dump util and looks like it should solve your problems

Skillless answered 27/8, 2008 at 1:28 Comment(1)
Your link is deadRida

© 2022 - 2024 — McMap. All rights reserved.