technovelty

weblog of Ian Wienand

RSS  |  technovelty home  |  page of ian  |  ian@wienand.org

hexdump format strings

More from the "things you'd learn if you read the manual" department, although the manual as currently written is a little esoteric on this feature of hexdump.

$ hexdump -e ' [iterations]/[byte_count] "[format string]" '

As you might guess, this means apply format string to groups of byte_count bytes, iterations times. Format string is like printf.

You can of course chain multiple formats together, or put them in a file. So say you needed to clag some binary data into a C array, a-la firmware for loading into a driver. You could use

$ hexdump -v -e '6/4 "0x%08x, "' -e '"\n"' ./dump

to get something that fits in 80 columns and is almost ready to go.

posted at: Tue, 13 Dec 2005 15:46 | in /linux/tips | permalink | add comment (4 others)

Posted by Daniel at Thu Aug 16 09:10:09 2007

Thanks a million. Please find some way to get your explanation in the hexdump man page. As of this writing, it does not adequately explain the -e option. Argh.

Posted by Andreas at Wed Mar 4 04:27:08 2009

I agree wholeheartedly with Daniel: please get this into the hexdump man page.

was wondering how to make hexdump -v -e '1/1 "\\\x"' -e '1/1 "%01x"'

=)

Posted by Malvineous at Wed Oct 6 21:30:55 2010

Thanks, now I can reproduce the -C option (roughly) like this:

hexdump -e '"%08_ax  "' -e ' 16/1 "%02x " ' -e '"  |"'  -e '16/1 "%_p" ' -e '"|\n"'

Only I can change it to a width other than 16 characters!

Posted by Chris at Tue Oct 12 10:33:55 2010

One thing I kept missing was that it seem to chop trailing spaces in the format string, so I was confused about adding extra spaces (note the 2 spaces in the string below).

@Malvineous  The actual -C format (according to the format strings in /usr/bin/hd is:

hexdump -e '"%08.8_ax  " 8/1 "%02x " "  " 8/1 "%02x "' -e'"  |" 16/1 "%_p" "|\n"'

Thanks all.

Add a comment
*Name
*Email (not shown)
Website
*Comment:
Anti-spam:
* denotes required field

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.