<p>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.</p>

<p>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?</p>

<p>Features I'd like:</p>

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

<p><strong>Edit:</strong>  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.</p>

[{'answer_id': 29254, 'author': 'Damien B', 'author_id': 3069, 'author_profile': 'https://Stackoverflow.com/users/3069', 'pm_score': 1, 'selected': False, 'text': '<p>Could you write your <a href="http://www.richardsharpe.com/ethereal-stuff.html#Writing%20a%20Dissector" rel="nofollow noreferrer">own dissector</a> for Wireshark?</p>\n\n<p>Edit: written before the precision in the question</p>\n'}, {'answer_id': 29279, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 1, 'selected': False, 'text': '<p>I have seen <a href="http://www.pspad.com/" rel="nofollow noreferrer">PSPad</a> 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.</p>\n'}, {'answer_id': 29320, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://gd.tuwien.ac.at/linuxcommand.org/man_pages/xxd1.html" rel="nofollow noreferrer">xxd</a> is the \'standard\' hex dump util and looks like it should solve your problems</p>\n'}, {'answer_id': 29395, 'author': 'nohat', 'author_id': 3101, 'author_profile': 'https://Stackoverflow.com/users/3101', 'pm_score': 4, 'selected': True, 'text': '<p>The unix tool <code>xxd</code> is distributed as part of <a href="http://www.vim.org/download.php" rel="noreferrer"><code>vim</code></a>, and according to <a href="http://www.vmunix.com/vim/util.html#xxd" rel="noreferrer">http://www.vmunix.com/vim/util.html#xxd</a>, the source for xxd is <a href="ftp://ftp.uni-erlangen.de:21/pub/utilities/etc/xxd-1.10.tar.gz" rel="noreferrer">ftp://ftp.uni-erlangen.de:21/pub/utilities/etc/xxd-1.10.tar.gz</a>. It was written in C and is about 721 lines. The only licensing information given for it is this:</p>\n\n<pre><code>* Distribute freely and credit me,\n* make money and share with me,\n* lose money and don\'t ask me.\n</code></pre>\n\n<p>The unix tool <code>hexdump</code> is available from <a href="http://gd.tuwien.ac.at/softeng/Aegis/hexdump.html" rel="noreferrer">http://gd.tuwien.ac.at/softeng/Aegis/hexdump.html</a>. 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.</p>\n'}, {'answer_id': 29865, 'author': 'epatel', 'author_id': 842, 'author_profile': 'https://Stackoverflow.com/users/842', 'pm_score': 6, 'selected': False, 'text': '<p>I often use this little snippet I\'ve written long time ago. It\'s short and easy to add anywhere when debugging etc...</p>\n\n<pre><code>#include &lt;ctype.h&gt;\n#include &lt;stdio.h&gt;\n\nvoid hexdump(void *ptr, int buflen) {\n  unsigned char *buf = (unsigned char*)ptr;\n  int i, j;\n  for (i=0; i&lt;buflen; i+=16) {\n    printf("%06x: ", i);\n    for (j=0; j&lt;16; j++) \n      if (i+j &lt; buflen)\n        printf("%02x ", buf[i+j]);\n      else\n        printf("   ");\n    printf(" ");\n    for (j=0; j&lt;16; j++) \n      if (i+j &lt; buflen)\n        printf("%c", isprint(buf[i+j]) ? buf[i+j] : \'.\');\n    printf("\\n");\n  }\n}\n</code></pre>\n'}, {'answer_id': 31012, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://einaros.blogspot.com/2006/11/put-hex-on-that-dump.html" rel="nofollow noreferrer">I used this</a> in one of my internal tools at work.</p>\n'}, {'answer_id': 19047008, 'author': 'Lupus', 'author_id': 1635516, 'author_profile': 'https://Stackoverflow.com/users/1635516', 'pm_score': 2, 'selected': False, 'text': '<p>Just in case someone finds it useful...</p>\n\n<p>I\'ve found single function implementation for ascii/hex dumper <a href="https://stackoverflow.com/questions/7775991/how-to-get-hexdump-of-a-structure-data/7776146#7776146">in this answer</a>.</p>\n\n<p>A C++ version based on the same answer with ANSI terminal colours can be found <a href="https://gist.github.com/shreyasbharath/32a8092666303a916e24a81b18af146b" rel="nofollow noreferrer">here</a>.</p>\n\n<p>More lightweight than xxd.</p>\n'}]
['https://Stackoverflow.com/questions/29242', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1175/']