Wednesday, 22 October 2014 18:29

How to create a 32bit word based hexdump on Linux

Written by 
Rate this item
(0 votes)

I have to work a lot with 32bit word aligned data. Unfortunately the hexdump tool included with linux uses byte or short alignment by default. I’ve wondered how to make hexdump to print 32bit words instead including an offset indicator as well as the hexdump typical ascii interpretation.

What I came up with was:

monte@mibix:~$ dd if=/dev/urandom bs=4 count=32 2>/dev/null | hexdump -v -e '"%08_ax: "' -e ' 4/4 "%08x " " |"' -e '16/1 "%_p" "|\n"'
00000000: 4d6ec4eb d5b8921d c3bd306a c0d20d1a |..nM....j0......|
00000010: 228d9a4c 4fd68ea7 6a837903 aca46e9a |L.."...O.y.j.n..|
00000020: 513e32f6 2cbc69db ca18bda0 d74bbe71 |.2>Q.i.,....q.K.|
00000030: b27cfd65 8859a08a db92a090 f81e301d |e.|...Y......0..|
00000040: 029a5e6c 23a3d621 ce35a06d e4b48cbd |l^..!..#m.5.....|
00000050: 2bba6da0 2e0c7f84 536eb6ad 94a4827b |.m.+......nS{...|
00000060: 17699fc1 2231d9d1 43210088 37f97a4c |..i...1"..!CLz.7|
00000070: e474aef3 23483fa7 b7f6ed41 cb01d18a |..t..?H#A.......|monte@mibix:~$ 

The dd command reads the data from a binary file. The block size (bs) is 4 so we read 32bit words. The count is 32 which means we read 128 bytes in total. We pipe stderr to null because we do not want to see the in/out statistic. The stdout output is forwarded to the hexdump command.

We specify the -v option to output all data as is. Otherwise hexdump would shorten equal data. The first -e format parameter tells hexdump to print the offset. The second -e format parameter tells hexump to print 4 times 4 bytes (which is 32bits) in the format with 8 nibbles and trailing zeros. The last -e format tells hexdump to print 16 times one byte as character interpretation. The %_p format will print a „.“ for each non printable character. Note that the last two -e formats print the same data. If you specify all formats with one -e you will get the wrong result.

Read 3456 times Last modified on Friday, 26 June 2015 12:28
More in this category: Windows Kernel Mode Wait »

Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.