Write hexadecimal to file

From Luniwiki
Jump to: navigation, search

By bash script

cat writehex.sh
   for arg; do
       for ((i=0; i<${#arg}; i+=2))
       do
           printf "\x${arg:i:2}"
       done
   done

Usage

To write hex values 4865 6c6c 6f20 576f 726c 64 (Hello World) to a file:

./writehex.sh 4865 6c6c 6f20 576f 726c 64 > hexfile
xxd hexfile
00000000: 4865 6c6c 6f20 576f 726c 64              Hello World

With xxd

echo "4865 6c6c 6f20 576f 726c 64" | xxd -r -p > hexfile
xxd hexfile
00000000: 4865 6c6c 6f20 576f 726c 64              Hello World 

References

Daniel Simao 14:50, 21 January 2020 (EST)