I have a binary that has to be copied over serial port to the device that has hexedit installed.
How to get hex dump of the binary on Linux (preferably in Python) in a format that can be simply inserted into hexedit?
CodePudding user response:
Restating the question a little, I think you want to generate a hex dump of a binary file on one machine and save it in the clipboard, then start hexedit
on a different machine and paste the clipboard into hexedit
- thereby transferring a binary file.
So, let's generate a recognisable, small PNG image which is a binary file, just using ImageMagick:
magick -size 64x64 xc:red -strip image.png
Now look at in hex:
xxd image.png
00000000: 8950 4e47 0d0a 1a0a 0000 000d 4948 4452 .PNG........IHDR
00000010: 0000 0040 0000 0040 0103 0000 0090 a7e3 ...@...@........
00000020: 9d00 0000 0350 4c54 45ff 0000 19e2 0937 .....PLTE......7
00000030: 0000 000f 4944 4154 28cf 6360 1805 a380 ....IDAT(.c`....
00000040: 7c00 0002 4000 018c c5ab 7700 0000 0049 |[email protected]
00000050: 454e 44ae 4260 82 END.B`.
That looks good, but we don't want all the address offsets down the left side, or the ASCII on the right side or the spaces and newlines, so we can use a "plain" dump:
xxd -p -c0 image.png
89504e470d0a1a0a0000000d494844520000004000000040010300000090a7e39d00000003504c5445ff000019e209370000000f4944415428cf63601805a3807c0000024000018cc5ab770000000049454e44ae426082
If you don't have xxd
, you can use od
instead, something like this:
od -An -t x1 image.png | tr -d ' \n'
And now we want to get that into our clipboard so we can paste it into the hexedit
on the remote machine. The command will vary between operating systems.
On macOS:
xxd -p -c0 image.png | pbcopy
On Linux, something like:
xxd -p -c0 image.png | xsel --clipboard
Or:
xxd -p -c0 image.png | xclip -sel clip
Then go to the remote machine, create an empty file called pasted.png
, start hexedit
and paste into the file, and save:
> pasted.png ; hexedit pasted.png
PASTE
Ctrl-X
PASTE is probably SHIFTINSERT, or middle mouse button, or CtrlV.
It looks like this in hexedit
:
Now open the (binary) file to check it:
xdg-open pasted.png