简体   繁体   English

使用 bash 显示和格式化对齐的二进制数据

[英]Display and format aligned binary data using bash

I have a binary file that stores a collection of C structs, eg我有一个存储 C 结构集合的二进制文件,例如

typedef struct{
    uint8_t field1;
    uint16_t field2;
    uint32_t field3;
}example;

I would like to dump the file aligned, ie have one instance per line.我想转储文件对齐,即每行一个实例。 I don't really need to have space separated values for each field, this would be enough for example:我真的不需要为每个字段使用空格分隔的值,例如:

# field 1 == 0xaa, field 2 == 0xbbcc, field 3 == 0x00112233
$ command my_file.bin
aabbcc00112233 # output is repeated for each struct

Considering the example above, file content is the following:考虑到上面的例子,文件内容如下:

$ hexdump my_file.bin
0000000 ccaa 33bb 1122 aa00 bbcc 2233 0011 ccaa
0000010 33bb 1122 aa00 bbcc 2233 0011 ccaa 33bb
0000020 1122 aa00 bbcc 2233 0011 ccaa 33bb 1122
0000030 aa00 bbcc 2233 0011 ccaa 33bb 1122 aa00
0000040 bbcc 2233 0011                         
0000046

od is a perfect fit when the struct is a multiple of 4 (eg od -tx --width=8 ), but does not work properly in this example where the width is 7 bytes.当结构是 4 的倍数(例如od -tx --width=8 )时, od 非常适合,但在宽度为 7 字节的示例中不能正常工作。 Is it possible in bash? bash 中可以吗?

Tell od to print 7 bytes per line, each individually, and get rid of spaces using tr .告诉od每行单独打印 7 个字节,并使用tr去除空格。

$ od -An -v -tx1 -w7 file | tr -d ' '
aabbcc00112233
...

Note that this is only good for big-endian inputs.请注意,这仅适用于大端输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM