现成的 C++ 十六进制转储代码
我经常使用网络和串行通信软件,因此我通常需要有代码来显示或记录数据包的十六进制转储。
每次执行此操作时,我都会从头开始编写另一个十六进制转储例程。 我正准备再次这样做,但我想我会在这里问:是否有任何好的免费 C++ 十六进制转储代码?
我想要的功能:
- 每行 N 个字节(其中 N 是可配置的)
- 可选的 ASCII/UTF8 转储以及十六进制
- 可配置缩进、每行前缀、每行后缀等
- 最小依赖性(理想情况下,我想要代码全部放在头文件中,或者是我可以粘贴的片段)
编辑:澄清:我正在寻找可以轻松放入我自己的程序以写入stderr的代码, stdout、日志文件或其他此类输出流。 我不是在寻找命令行十六进制转储实用程序。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我经常使用我很久以前写的这个小片段。 它简短且易于在调试等任何地方添加...
I often use this little snippet I've written long time ago. It's short and easy to add anywhere when debugging etc...
unix 工具
xxd
作为vim
< 的一部分分发/a>,并根据 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 行。 为其提供的唯一许可信息是:unix 工具
hexdump
可从 http://gd.tuwien.ac.at/softeng/Aegis/hexdump.html。 它是用 C 编写的,可以从源代码编译。 它比 xxd 大很多,并且在 GPL 下分发。The unix tool
xxd
is distributed as part ofvim
, 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: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.以防万一有人发现它有用...
我发现了 ascii/hex dumper 的单一函数实现 在此答案中。
可以在此处找到基于相同答案和 ANSI 终端颜色的 C++ 版本。
比xxd更轻。
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.
我见过 PSPad 用作十六进制编辑器,但我通常会做与您相同的事情。 我很惊讶这个问题没有“即时答案”。 这是一个非常普遍的需求。
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.
我在我的内部之一中使用了这个工作中的工具。
I used this in one of my internal tools at work.
您能为 Wireshark 编写自己的解析器吗?
编辑:写在问题的精度之前
Could you write your own dissector for Wireshark?
Edit: written before the precision in the question
xxd 是“标准”十六进制转储实用程序,看起来它应该可以解决您的问题
xxd is the 'standard' hex dump util and looks like it should solve your problems