I was wondering if anybody knew of a command line utility that would allow me to directly write hex values to a file. such that when a hexdump of the file is done the values that I enter would be spit out. It is vital that I write directly to hex as many of the value that I need to write do not have any unicode or ascii equivalent characters associated with them. Looking for something like:
writehex f6050000 ac7e0500 02000800 01000000 newfile
hexdump newfile
hexdump 1.02
ts.event.1
00000000: f6050000 ac7e0500 02000800 01000000 .....~..........
16 bytes read
This Bash function should work for you:
Alternative implementation:
To test it:
Another option would be to use
xxd
.Edit:
Additionally, you can use the
-p
option to accept somewhat freeform input data:Edit 2:
Modified function above to handle multiple input arguments (strings of hex digits separated by spaces).
With
perl
:Where the content of
inputfile
is formatted:outputfile
would then containBCD
. You can prep your input file invi
like so:That will remove all spaces, then insert a space between every character.
The example you gave using that
perl
line:What about a hex editor, like hexedit ou hexer? You can directly edit the file and type values in hexadecimal with them.
Just had the same challenge and found this solution. I tried to go with the solution from Dennis Williamson but it was very slow when implemented in a mass operation (had to convert ~60k sqlite blobs into files). I came up with a reimplementation of
writehex
which I wanted to share here:When it comes to performance the pro of it is that it just uses a single call to sed and a lightweight regex. Your mileage may vary but I found it to be much faster.