我经常使用网络和串行通信软件,因此我通常需要有代码来显示或记录数据包的十六进制转储。
每次执行此操作时,我都会从头开始编写另一个十六进制转储例程。我正准备再次这样做,但我想我会在这里问:是否有任何好的免费 C++ 十六进制转储代码?
我想要的功能:
编辑:澄清:我正在寻找可以轻松放入我自己的程序中以写入stderr、stdout、日志文件或其他此类输出流的代码。我不是在寻找命令行十六进制转储实用程序。
我经常使用我很久以前写的这个小片段。它简短且易于在调试等任何地方添加...
#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");
}
}
unix 工具
xxd
作为 vim
的一部分进行分发,根据 http://www.vmunix.com/vim/util.html#xxd,xxd 的源是 ftp:/ /ftp.uni-erlangen.de:21/pub/utilities/etc/xxd-1.10.tar.gz。它是用 C 语言编写的,大约有 721 行。为其提供的唯一许可信息是:
* Distribute freely and credit me,
* make money and share with me,
* lose money and don't ask me.
unix 工具
hexdump
可从 http://gd.tuwien.ac.at/softeng/Aegis/hexdump.html 获得。它是用 C 编写的,可以从源代码编译。它比 xxd 大很多,并且是在 GPL 下分发的。
我在工作中的一个内部工具中使用了这个。
自己的解剖器吗?
编辑:写在问题的精度之前