How to get specific format using hexdump or xxd?

ICEpower picture ICEpower · Aug 8, 2019 · Viewed 6.9k times · Source

I require the hexdumps of a number of files in a specific format:

00000000 4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00

However, using hexdump or xxd I can only manage to get the above with a colon after the address and the ASCII text to the right of it, e.g.:

00000000: 4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 
MZ..............

I got the above using the command

xxd -g 1 -u filename

Any ideas?

Answer

Shawn picture Shawn · Aug 8, 2019

od is one way:

After creating an example file via

perl -e 'print map { chr hex } @ARGV' 4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00 > foo.bin

getting a hex dump of it:

$ od -Ax -t x1 foo.bin
0000000 4d 5a 90 00 03 00 00 00 04 00 00 00 ff ff 00 00
0000010

The key here is the -t FORMAT argument. In the format, x uses base 16, and 1 means to print one byte per block. The -Ax says to print out the offsets in base 16 instead of the default base 8.

It does print out the offset of the end of the file as the last line, but that's trivial to get rid of with head -n -1 if not needed. There doesn't seem to be a way to make it use upper-case hex digits, but that's also easily fixable if you prefer them:

$ od -Ax -t x1 foo.bin | head -n -1 | tr a-f A-F
000000 4D 5A 90 00 03 00 00 00 04 00 00 00 FF FF 00 00