Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell magic wanted: format output of hexdump in a pipe

I'm debugging the output of a program that transmits data via TCP. For debugging purposes i've replaced the receiving program with netcat and hexdump:

netcat -l -p 1234 | hexdump -C

That outputs all data as a nice hexdump, almost like I want. Now the data is transmitted in fixed blocks which lengths are not multiples of 16, leading to shifted lines in the output that make spotting differences a bit difficult:

00000000  50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00000010  00 50 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |.P..............|
00000020  00 00 50 00 00 00 00 00  00 00 00 00 00 00 00 00  |..P.............|

How do I reformat the output so that after 17 bytes a new line is started? It should look something like this:

50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |
50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |
50 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |P...............|
00                                                |.               |

Using hexdumps -n parameter does not work since it will exit after reaching the number of bytes. (Unless there is a way to keep the netcat programm running and seamlessly piping the next bytes to a new instance of hexdump).

Also it would be great if I could use watch -d on the output to get a highlight of changes between lines.

like image 546
grimmig Avatar asked May 12 '11 07:05

grimmig


People also ask

How do I read a Hexdump file?

The address of a hex dump counts tracks the number of bytes in the data and offsets each line by that number. So the first line starts at offset 0, and the second line represents the number 16, which is how many bytes precede the current line.

What is Hexdump of a file?

In computing, a hex dump is a hexadecimal view (on screen or paper) of computer data, from memory or from a computer file or storage device. Looking at a hex dump of data is usually done in the context of either debugging or reverse engineering.

What is XXD in Hexdump?

DESCRIPTION. xxd creates a hex dump of a given file or standard input. It can also convert a hex dump back to its original binary form. Like uuencode(1) and uudecode(1) it allows the transmission of binary data in a `mail-safe' ASCII representation, but has the advantage of decoding to standard output.


1 Answers

For hexdump without characters part.

hexdump -e '16/1 "%0.2x " "\n" 1/1 "%0.2x " "\n"'

like image 53
Timofey Stolbov Avatar answered Oct 04 '22 02:10

Timofey Stolbov