简体   繁体   中英

Display and format aligned binary data using bash

I have a binary file that stores a collection of C structs, eg

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. Is it possible in bash?

Tell od to print 7 bytes per line, each individually, and get rid of spaces using tr .

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

Note that this is only good for big-endian inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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