Writing hex values to a file (not as ascii values) via the command line
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:
writehex ()
{
local i
while [ "$1" ]; do
for ((i=0; i<${#1}; i+=2))
do
printf "\x${1:i:2}";
done;
shift;
done
}
Alternative implementation:
writehex ()
{
local arg, i
for arg; do
for ((i=0; i<${#arg}; i+=2))
do
printf "\x${arg:i:2}"
done
done
}
To test it:
$ writehex abc00001ff | hexdump -C
00000000 ab c0 00 01 ff |.....|
00000005
$ writehex f6050000 ac7e0500 02000800 01000000 | hexdump -C
00000000 f6 05 00 00 ac 7e 05 00 02 00 08 00 01 00 00 00 |.....~..........|
00000010
Another option would be to use xxd
.
$ echo hello | xxd
0000000: 6865 6c6c 6f0a
$ echo hello | xxd | xxd -r
hello
Edit:
Additionally, you can use the -p
option to accept somewhat freeform input data:
$ echo 'f6050000 ac7e0500 02000800 01000000' | xxd -r -p | hexdump -C
00000000 f6 05 00 00 ac 7e 05 00 02 00 08 00 01 00 00 00 |.....~..........|
00000010
Edit 2:
Modified function above to handle multiple input arguments (strings of hex digits separated by spaces).
With perl
:
perl -ne '@a=split;for(@a){print chr(hex($_))}' inputfile > outputfile
Where the content of inputfile
is formatted:
42 43 44
outputfile
would then contain BCD
. You can prep your input file in vi
like so:
:%s/ //g
:%s/\(.\{2\}\)/\1 /g
That will remove all spaces, then insert a space between every character.
The example you gave using that perl
line:
$ cat inputfile
f6 05 00 00 ac 7e 05 00 02 00 08 00 01 00 00 00
$ hexdump outputfile
0000000 f6 05 00 00 ac 7e 05 00 02 00 08 00 01 00 00 00
0000010
What about a hex editor, like hexedit ou hexer? You can directly edit the file and type values in hexadecimal with them.