现成的 C++ 十六进制转储代码

2021-12-19 00:00:00 logging hex c++ hexdump

我经常使用网络和串行通信软件,所以我经常需要代码来显示或记录数据包的十六进制转储.

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.

每次我这样做时,我都会从头开始编写另一个十六进制转储例程.我正要再次这样做,但我想我会在这里问:在某处有没有好的免费的 C++ 十六进制转储代码?

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?

我想要的功能:

  • 每行 N 个字节(其中 N 以某种方式可配置)
  • 可选的 ASCII/UTF8 转储与十六进制一起
  • 可配置缩进、每行前缀、每行后缀等
  • 最小的依赖(理想情况下,我希望代码都在头文件中,或者是我可以粘贴的片段)

说明:我正在寻找可以轻松放入我自己的程序以写入 stderr、stdout、日志文件或其他此类输出流的代码.我不是在寻找命令行十六进制转储实用程序.

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.

推荐答案

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 行.为其提供的唯一许可信息如下:

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.

unix 工具 hexdump 可从 http://gd.tuwien.ac.at/softeng/Aegis/hexdump.html.它是用 C 编写的,可以从源代码编译.它比 xxd 大很多,并且是在 GPL 下分发的.

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.

相关文章