A blog by Devendra Tewari
In this post I’ll mention how to edit binary files using vi and the utility xxd that is a part of vi.
Use the xxd command by typing :%!xxd
. Edit hex data. Quit hex mode with :%!xxd -r
.
You can use the xxd command outside vi. If you have an existing binary file, you can convert it to hex
xxd -g 1 file.bin > file.hex
You can then edit the hex and convert it back to binary
xxd -r file.hex > file.bin
You can pipe standard input to xxd to convert hex to binary
echo "dead bead" | xxd -p -r > file.bin
Certain minimal versions of xxd require lowercase input - you can use sed to perform the conversion
echo "DEAD BEAD" | sed y/ABCDEF/abcdef/ | xxd -p -r > file.bin
Certain minimal versions of xxd require hex with 30 bytes per line - sed can help with that
echo "5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a5a" | sed 's/.\{60\}/&\n/g' | xxd -p -r > file.bin
One nice little thing that xxd does is to produce a C static array definition, very convenient for embedding resource files
xxd -i file.bin