0

I have a binary file that stores a collection of C structs, e.g.

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

I would like to dump the file aligned, i.e. 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 (e.g. od -tx --width=8), but does not work properly in this example where the width is 7 bytes. Is it possible in bash ?

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.