Searching through a hex dump using regex in Vim (or elsewhere)

后端 未结 3 1020
孤街浪徒
孤街浪徒 2021-01-15 12:59

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:



        
3条回答
  •  抹茶落季
    2021-01-15 13:19

    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
    

提交回复
热议问题