I’m looking for a way to search for the text representation of a series of hexadecimal numbers. I search in the hex dump of a binary file that looks like so:
Let me propose the following mappings that take a number of hex digits from user input or visual selection, create appropriate pattern, and start a search for it.
nnoremap x/ SearchHexBytes('/', 0)
nnoremap x? SearchHexBytes('?', 0)
vnoremap x/ :call SearchHexBytes('/', 1)/
vnoremap x? :call SearchHexBytes('?', 1)?
function! SearchHexBytes(dir, vis)
if a:vis
let [qr, qt] = [getreg('"'), getregtype('"')]
norm! gvy
let s = @"
call setreg('"', qr, qt)
else
call inputsave()
let s = input(a:dir)
call inputrestore()
endif
if s =~ "[^ \t0-9A-Fa-f]"
echohl Error | echomsg 'Invalid hex digits' | echohl None
return
endif
let @/ = join(split(s, '\s\+'), '\%(\s*\|\n0x\x\+:\s*\)')
return a:dir . "\r"
endfunction