注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

dp: 生活的脚步,进步的点滴...

Cam、DSP、FPGA、PM、Life、More ...

 
 
 

日志

 
 

vim二进制与十六进制转换插件binary.vim  

2012-02-02 14:53:43|  分类: 专业 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
" Vim plugin -- deal with binary numbers in the text
" File: binary.vim
" Created: 2009 Jun 16
" Last Change: 2009 Jun 16
" Rev Days: 0
" Author: Andy Wokula <anwoku@yahoo.de>


" Script Init Folklore: {{{
if exists("loaded_binary")
finish
endif
let loaded_binary = 1

if v:version < 700
echomsg "Binary: you need at least Vim 7.0"
finish
endif
"}}}


" Config Variables:

" minimum length of the result: 1:byte, 2:word, 4:dword, ...
" g:binary_octets_min {{{
if !exists("g:binary_octets_min")
let g:binary_octets_min = 1
endif
"}}}
" add "b" to the binary number in the text?
" g:binary_suffix {{{
if !exists("g:binary_suffix")
let g:binary_suffix = "b"
" if longer than one char, the test pattern in GetBinvac() needs a
" change
endif
"}}}


" Mappings:

" Default Keys {{{
if !hasmapto("<Plug>IncBinary")
nmap <unique> <Leader>ba <Plug>IncBinary
endif
if !hasmapto("<Plug>DecBinary")
nmap <unique> <Leader>bx <Plug>DecBinary
endif
"}}}
" Interface Mappings {{{

" increase a binary number at the cursor
nnoremap <silent> <Plug>IncBinary
\ :<C-U>call SetBinvac(GetBinvac()+v:count1)<CR>

" decrease a binary number at the cursor
nnoremap <silent> <Plug>DecBinary
\ :<C-U>call SetBinvac(GetBinvac()-v:count1)<CR>
"}}}


" Functions:

" GetBinaryValueAtCursor(), returns a number
func! GetBinvac() "{{{
let wordac = expand("<cword>")
if wordac !~ '^[01]\+\D\=$'
throw "Not a binary number at cursor"
endif
return eval("0x".Bin2Hex(wordac))
endfunc "}}}

" SetBinaryValueAtCursor(), replaced string in the unnamed register
func! SetBinvac(number) "{{{
let fmtstr = "%0".(2*g:binary_octets_min)."X"
let binarystr = Hex2Bin(printf(fmtstr, a:number))
call StartOfWord()
exec "normal! ciw\<C-R>=binarystr\r". g:binary_suffix
endfunc "}}}

" return the first run of binary digits found in string {bin} converted
" to (lower case) hex digits
func! Bin2Hex(bin) "{{{
let bin = matchstr(a:bin, '[01]\+')
if bin == ""
return "0"
endif
let bin = repeat("0", PadSize(strlen(bin), 4)). bin
let runs = split(bin, '....\zs')
call map(runs, 's:fourbd2hd(v:val)')
return join(runs, '')
endfunc "}}}

" return the first run of hex digits in string {hex} converted into
" binary digits (a match at "0x" is skipped)
func! Hex2Bin(hex) "{{{
let hex = matchstr(a:hex, '\%(0x\)\@!\x\+')
if hex == ""
return "0"
endif
return join(map(split(hex, '\m'), 's:hd2fourbd(v:val)'), '')
endfunc "}}}


" Lib Functions: functions that could be extracted

" calculate number of pad bytes to make {len} a multiple of {chunksize}
func! PadSize(len, chunksize) "{{{
return -1 + (a:chunksize - (a:len + a:chunksize - 1) % a:chunksize)
endfunc
" padsize := PadSize(len,chunksize) <=>
" 0 <= padsize < chunksize AND (len + padsize) mod chunksize = 0
"}}}

" move cursor to Start of word (the word returned by expand("<cword>"))
func! StartOfWord() "{{{
let xw = expand("<cword>")
if xw != ""
if xw =~ '\k'
call search('\k\>','cW')
else
call search('\S\S\@!','cW')
endif
if strlen(xw) > 1
normal! b
endif
endif
endfunc "}}}


" Local Functions:

" convert 4 bin digits (string) into 1 (lower case) hex digit
func! s:fourbd2hd(bd) "{{{
let bd = a:bd
return printf("%x", 8*bd[0] + 4*bd[1] + 2*bd[2] + bd[3])
endfunc "}}}

" convert 1 hex digit (string) into 4 bin digits
func! s:hd2fourbd(hd) "{{{
let hd = eval("0x". a:hd)
return (hd/8) . (hd/4%2) . (hd/2%2) . (hd%2)
endfunc "}}}



" Modeline: {{{1
" NOTE: must not use substitute() in the functions to avoid recursive
" submatch() when called from :s/.../\=.../
" substitute(bin,'[01]\{4}', '\=s:fourbd2hd(submatch(0))', 'g')
" substitute(a:hex, '\x', '\=s:hd2fourbd(submatch(0))', 'g')

" vim:tw=72 et fdm=marker:
  评论这张
 
阅读(716)| 评论(0)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2016