Saturday, March 15, 2008

Edit binary file in hex mode in vim

It will be nice if we can edit binary file directly in vim. VIM provides basic support which has a few restrictions.
(1) Open a binary file
    vim -b datafile
or
    :set binary
(2) Many characters are unprintable. You can see the Hex format by using:
    :set display=uhex
Or you can use ga command to see the value of current character.
(3) To see current position, use
    g CTRL-G
The output is verbose:
    Col 6 of 38; Line 31 of 31; Word 94 of 96; Byte 747 of 780
(4) Move to a specific byte offset:
    234go
(5) xxd can be used to convert the file into hex dump format
    %!xxd
Result should look like this:   
    0000000: 6262 630a 6465 660a 6768 696b 0aab de0a  bbc.def.ghik....
There are two parts: hex part and printable character part.
Go back:
    %!xxd -r
Note: only changes in hex part have effect. Changes in printable text part are ignored.

Of course, tool xxd can be used independently in command line.

3 comments:

pzy said...

Useful tips! thanks.

filipenf said...

i need to handle files that are mixed ( long texts with some chunks of binary in the middle ). I wonder if there's a way to only xxd the selected block of bytes ( about 40~150 bytes in a 3mb file ). Do you know any way to do that ?

Thanks

sujithkumar said...

i need how to convert .exe to .hex file in linux?
this problem belongs to qt creator
what are the commands used for conversion .exe to .hex file?