[SATLUG] searching for a hexvalue with vi
scarolan at gmail.com
Thu Dec 20 10:27:45 CST 2007
> Yes, I need to know how to search for an arbitrary hex char. I know how to search for \r and \n
> like this, but I need to know how to do it for any non-ASCII character. I don't care if I
> search for it in its octal, decimal or hexadecimal representation.
If you have vim installed, this might be helpful:
*23.4* Binary files
You can edit binary files with Vim. Vim wasn't really made for this, thus
there are a few restrictions. But you can read a file, change a character and
write it back, with the result that only that one character was changed and
the file is identical otherwise.
To make sure that Vim does not use its clever tricks in the wrong way, add
the "-b" argument when starting Vim:
vim -b datafile
This sets the 'binary' option. The effect of this is that unexpected side
effects are turned off. For example, 'textwidth' is set to zero, to avoid
automatic formatting of lines. And files are always read in Unix file format.
Binary mode can be used to change a message in a program. Be careful not to
insert or delete any characters, it would stop the program from working. Use
"R" to enter replace mode.
Many characters in the file will be unprintable. To see them in Hex format:
Otherwise, the "ga" command can be used to see the value of the character
under the cursor. The output, when the cursor is on an <Esc>, looks like
<^[> 27, Hex 1b, Octal 033
There might not be many line breaks in the file. To get some overview switch
the 'wrap' option off:
To see on which byte you are in the file use this command:
The output is verbose:
Col 9-16 of 9-16; Line 277 of 330; Word 1806 of 2058; Byte 10580 of 12206
The last two numbers are the byte position in the file and the total number of
bytes. This takes into account how 'fileformat' changes the number of bytes
that a line break uses.
To move to a specific byte in the file, use the "go" command. For
example, to move to byte 2345:
A real binary editor shows the text in two ways: as it is and in hex format.
You can do this in Vim by first converting the file with the "xxd" program.
This comes with Vim.
First edit the file in binary mode:
vim -b datafile
Now convert the file to a hex dump with xxd:
The text will look like this:
0000000: 1f8b 0808 39d7 173b 0203 7474 002b 4e49 ....9..;..tt.+NI
0000010: 4b2c 8660 eb9c ecac c462 eb94 345e 2e30 K,.`.....b..4^.0
0000020: 373b 2731 0b22 0ca6 c1a2 d669 1035 39d9 7;'1.".....i.59.
You can now view and edit the text as you like. Vim treats the information as
ordinary text. Changing the hex does not cause the printable character to be
changed, or the other way around.
Finally convert it back with:
Only changes in the hex part are used. Changes in the printable text part on
the right are ignored.
See the manual page of xxd for more information.
More information about the SATLUG