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

发布于 2024-07-05 18:29:34 字数 410 浏览 12 评论 0原文

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

每次执行此操作时,我都会从头开始编写另一个十六进制转储例程。 我正准备再次这样做,但我想我会在这里问:是否有任何好的免费 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

烟若柳尘 2024-07-12 18:29:34

我经常使用我很久以前写的这个小片段。 它简短且易于在调试等任何地方添加...

#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");
  }
}

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");
  }
}
云巢 2024-07-12 18:29:34

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

* 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 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.

划一舟意中人 2024-07-12 18:29:34

以防万一有人发现它有用...

我发现了 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.

孤者何惧 2024-07-12 18:29:34

我见过 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.

水水月牙 2024-07-12 18:29:34

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

猛虎独行 2024-07-12 18:29:34

您能为 Wireshark 编写自己的解析器吗?

编辑:写在问题的精度之前

Could you write your own dissector for Wireshark?

Edit: written before the precision in the question

罗罗贝儿 2024-07-12 18:29:34

xxd 是“标准”十六进制转储实用程序,看起来它应该可以解决您的问题

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文