Edit binary files in Linux

In this post I’ll mention how to edit binary files using vi and the utility xxd that is a part of vi.

vi in hex mode

Use the xxd command by typing

:%!xxd

Edit hex data. Quit hex mode with

:%!xxd -r

Using xxd command

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

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