-
[20210810]完善vim bccalc_win插件.txt
[20210810]完善vim bccalc_win插件.txt
--//前一段时间改写vim bccalc_win插件,发现使用\16x计算取模时存在问题,计算错误。
--//建议不采用bc -l模式计算,采用bc模式计算.
--//测试如下:
10210a5e = 270600798
270600798 % 131072 %7+1 = 4 ===> 使用bc计算 \c0
10210a5e % 20000 % 7 +1 = 0x4 ===> 使用bc计算 \16x
--//以下计算错误:
10210a5e % 20000 % 7 +1 = 0x1 ===> 使用bc -l计算 \16x
270600798 % 131072 %7+1 = 1 ===> 使用bc -l计算 \cc
10210a5e % 20000 % 7 +1 = 1 ===> 使用bc -l计算 \10
--//顺便加入更多的注解,避免以后自己都读不懂源代码。在读代码时,我还发现源作者有一个插入模式计算方式,
--//实际上以前blog的上也有说明:
inoremap =: = <Esc>"eyy:call CalcLines(0)<CR>a
--//在输入表达式时输入=:就可以知道结果,当然输入要快。例子:"
12*4.2= 50.4
--//如果无法计算,显示如下,这种模式自己不太喜欢,仅仅加入=- (10to16,16to10的转换),其他不作扩展了。
123123a= (standard_in) 1: parse error
--//如果输入太慢就是如下,另外注意set paste问题,这样在插入模式无效。
12*4.2=:
--//另外为了代码的通用性修改加入如下:
if has("windows")
let str = substitute (str, '\^', '^^^^', "g")
endif
--//windows的命令行有一些变态,必须输入4个^才表示1个^.
D:\>echo 2^5 | bc
25
D:\>echo 2^^^^5 | bc
32
--//另外取消了提示行显示结果的需求,结果直接输出并且自动判断是否在结尾输入=.没有输入自动补上,整个改动有点大。
" append answer or echo
if has_equal == 1
exec "normal a " . answer
else
exec "normal a " . "= " . answer
"" echo "answer = " . answer
endif
--//源代码如下,bc.exe可执行程序自己要另行安装,一般我选择unxutils包.另外我测试仅仅<Leader>开头的命令。
$ cat vim bccalc_win.vim
"" calculate expression entered on command line and give answer, e.g.:
"" :Calculate sin (3) + sin (4) ^ 2
command! -nargs=+ Calculate echo "<args> = " . Calculate ("<args>",0)
"" calculate expression from selection using bc -l, pick a mapping, or use the Leader form
vnoremap ;bc "ey`>:call CalcLines(0)<CR>
vnoremap ;bb "ey`>:call CalcLines(0)<CR>
"" convert hexdecimal to decimal
vnoremap ;10 "ey`>:call CalcLines(10)<CR>
"" convert hexdecimal to decimal
vnoremap ;16 "ey`>:call CalcLines(16)<CR>
"" computer hexdecimal to hexdecimal
vnoremap ;16x "ey`>:call CalcLines(1016)<CR>
"" split event P1 to TYPE and MODE
vnoremap ;tx "ey`>:call CalcLines(1616)<CR>
"" split dba(10) or dba(16) to file# and block#
vnoremap ;22 "ey`>:call CalcLines(22)<CR>
vnoremap ;dba "ey`>:call CalcLines(22)<CR>
"" split scn(10) or scn(16) into scn_wrap,scn_base
vnoremap ;32 "ey`>:call CalcLines(32)<CR>
vnoremap ;scn "ey`>:call CalcLines(32)<CR>
"" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
vnoremap ;ss "ey`>:call CalcLines(10016)<CR>
"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
vnoremap ;rr "ey`>:call CalcLines(20016)<CR>
"" convert hexdecimal to decimal or decimal to hexdecimal
vnoremap ;hd "ey`>:call CalcLines(30016)<CR>
vnoremap ;hh "ey`>:call CalcLines(30016)<CR>
vnoremap ;dh "ey`>:call CalcLines(30016)<CR>
"" --------------------------------------------------------------------
"" calculate expression on current line using bc -l, pick a mapping, or use the Leader
nnoremap <Leader>bx <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap <Leader>bc <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap <Leader>bb <Esc>"eyy$:call CalcLines(0)<CR>
noremap <Leader>cc Yp!!bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J
"" calculate expression on current line using bc , pick a mapping, or use the Leader
noremap <Leader>c0 Yp!!bc -q\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J
"" calculate expression on current line ( delete ,) using bc -l, pick a mapping, or use the Leader
noremap <Leader>c, Yp!!sed "s/,//g" \|bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J
"" convert hexdecimal to decimal
nnoremap <Leader>10 <Esc>"eyy$:call CalcLines(10)<CR>
"" convert decimal to hexdecimal
nnoremap <Leader>16 <Esc>"eyy$:call CalcLines(16)<CR>
"" computer hexdecimal to hexdecimal
nnoremap <Leader>16x <Esc>"eyy$:call CalcLines(1016)<CR>
"" split event P1 to TYPE and MODE
nnoremap <Leader>tx <Esc>"eyy$:call CalcLines(1616)<CR>
"" split dba(10) or dba(16) to file# and block#
nnoremap <Leader>22 <Esc>"eyy$:call CalcLines(22)<CR>
nnoremap <Leader>dba <Esc>"eyy$:call CalcLines(22)<CR>
"" split scn(10) or scn(16) into scn_wrap,scn_base
nnoremap <Leader>32 <Esc>"eyy$:call CalcLines(32)<CR>
nnoremap <Leader>scn <Esc>"eyy$:call CalcLines(32)<CR>
"" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
nnoremap <Leader>ss <Esc>"eyy$:call CalcLines(10016)<CR>
"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
nnoremap <Leader>rr <Esc>"eyy$:call CalcLines(20016)<CR>
"" convert hexdecimal to decimal or decimal to hexdecimal
nnoremap <Leader>hd <Esc>"eyy$:call CalcLines(30016)<CR>
nnoremap <Leader>hh <Esc>"eyy$:call CalcLines(30016)<CR>
nnoremap <Leader>dh <Esc>"eyy$:call CalcLines(30016)<CR>
"" --------------------------------------------------------------------
"" calculate from insertmode
inoremap =: =<Esc>"eyy$:call CalcLines(0)<CR>a
inoremap =- =<Esc>"eyy$:call CalcLines(30016)<CR>a
"" --------------------------------------------------------------------
"" Calculate:
"" clean up an expression, pass it to bc, return answer
function! Calculate (s,flag)
let has_hex = 0
let str = a:s
" remove newlines and trailing spaces
let str = substitute (str, "\n", "", "g")
let str = substitute (str, '\s*$', "", "g")
" sub common func names for bc equivalent
let str = substitute (str, '\csin\s*(', 's (', 'g')
let str = substitute (str, '\ccos\s*(', 'c (', 'g')
let str = substitute (str, '\catan\s*(', 'a (', 'g')
let str = substitute (str, "\cln\s*(", 'l (', 'g')
let str = substitute (str, '\clog\s*(', 'l (', 'g')
let str = substitute (str, '\cexp\s*(', 'e (', 'g')
" alternate exponitiation symbols
let str = substitute (str, '\*\*', '^', "g")
let str = substitute (str, '`', '^', "g")
if has("windows")
let str = substitute (str, '\^', '^^^^', "g")
endif
" escape chars for shell
if has("unix")
let str = escape (str, '*();&><|^')
endif
let preload = exists ("g:bccalc_preload") ? g:bccalc_preload : ""
" run bc
" return str
" let answer = system ("echo " . str . " \| bc -l " . preload)
if a:flag == 0
let answer = system ("echo " . str . " \| bc -l " . preload)
" let answer = answer . " --- ". str
endif
if a:flag == 10
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
let answer = system ("echo ibase=16 ;" . str . " \| bc -l " . preload)
endif
if a:flag == 16
let answer = system ("echo obase=16 ;" . str . " \| bc -l " . preload)
let answer = "0x" . tolower ( answer )
endif
if a:flag == 1016
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
let answer = system ("echo obase=16 ;ibase =16;" . str . " \| bc " . preload)
let answer = "0x" . tolower ( answer )
endif
let has_hex = Check_hex(str)
if a:flag == 1616
if has_hex == 1
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
" 0x10000 hexdecimal = 65536 (10) = 2^16(10)
let answer = system ("echo ibase=16 ;" . str . "/10000" . " \| bc " . preload)
let answer1 = system ("echo ibase=16 ;" . str . "%10000" . " \| bc " . preload)
let answer2 = system ("echo ibase=16 ;" . str . " \| bc -l " . preload)
else
let answer = system ("echo " . str . "/65536" . " \| bc " . preload)
let answer1 = system ("echo " . str . "%65536" . " \| bc " . preload)
let answer2 = "0x" . system ("echo obase=16 ;" . str . " \| bc -l " . preload)
endif
let answer = "/2^16 %2^16 (Type | Mode) = " . answer . "," . answer1 ." = " . tolower(answer2)
endif
if a:flag == 22
if has_hex == 1
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
" 0x400000 hexdecimal = 4194304 (10) = 2^22(10)
let answer = system ("echo ibase=16 ;" . str . "/400000" . " \| bc " . preload)
let answer1 = system ("echo ibase=16 ;" . str . "%400000" . " \| bc " . preload)
let answer2 = system ("echo ibase=16 ;" . str . " \| bc -l " . preload)
else
let answer = system ("echo " . str . "/4194304" . " \| bc " . preload)
let answer1 = system ("echo " . str . "%4194304" . " \| bc " . preload)
let answer2 = "0x" . system ("echo obase=16 ;" . str . " \| bc -l " . preload)
endif
" let answer = "set dba " . answer . "," . answer1
let answer = "set dba " . answer . "," . answer1 ." = alter system dump datafile " . answer . " block " . answer1 ." = " . tolower(answer2)
endif
if a:flag == 32
if has_hex == 1
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
" 0x100000000 hexdecimal = 4294967296(10) = 2^32(10)
let answer = system ("echo ibase=16 ;" . str . "/100000000" . " \| bc " . preload)
let answer1 = system ("echo ibase=16 ;" . str . "%100000000" . " \| bc " . preload)
let answer2 = system ("echo obase=16 ;ibase=16 ;" . str . "/100000000" . " \| bc " . preload)
let answer3 = system ("echo obase=16 ;ibase=16 ;" . str . "%100000000" . " \| bc " . preload)
else
let answer = system ("echo " . str . "/4294967296" . " \| bc " . preload)
let answer1 = system ("echo " . str . "%4294967296" . " \| bc " . preload)
let answer2 = system ("echo obase=16 ;" . str . "/4294967296" . " \| bc " . preload)
let answer3 = system ("echo obase=16 ;" . str . "%4294967296" . " \| bc " . preload)
endif
let answer = "scn_wrap,scn_base(10): " . answer . "," . answer1 . " = scn_wrap,scn_base(16): " . "0x" . tolower (answer2) . "," . "0x" . tolower(answer3)
endif
if a:flag == 10016
if has_hex == 1
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
" 0x100000000 hexdecimal = 4294967296(10) = 2^32(10)
let str = substitute (str, "[,.]", "*100000000+", "g")
let answer = system ("echo obase=10 ;ibase=16 ;" . str . " \| bc -l " . preload)
let answer1 = system ("echo obase=16 ;ibase=16 ;" . str . " \| bc -l " . preload)
else
let str = substitute (str, "[,.]", "*4294967296+", "g")
let answer = system ("echo " . str . " \| bc -l " . preload)
let answer1 = system ("echo obase=16 ;" . str . " \| bc -l " . preload)
endif
let answer = "scn(10): " . answer . " = scn(16): " . "0x" . tolower (answer1)
endif
if a:flag == 20016
if has_hex == 1
let str = toupper ( str )
let str = substitute (str, "0x", "", "g")
" 0x400000 hexdecimal = 4194304 (10) = 2^22(10)
let str = substitute (str, "[,.]", "*400000+", "g")
let answer = system ("echo obase=10 ;ibase=16 ;" . str . " \| bc -l " . preload)
let answer1 = system ("echo obase=16 ;ibase=16 ;" . str . " \| bc -l " . preload)
else
let str = substitute (str, "[,.]", "*4194304+", "g")
let answer = system ("echo " . str . " \| bc -l " . preload)
let answer1 = system ("echo obase=16 ;" . str . " \| bc -l " . preload)
endif
let answer = "file#,block# dba(10): " . answer . " = file#,block# dba(16): " . "0x" . tolower (answer1)
endif
if a:flag == 30016
if has_hex == 1
let str = substitute (str, "0x", "", "g")
let str = toupper ( str )
let answer = system ("echo ibase=16 ;" . str . " \| bc -l " . preload)
else
let answer = system ("echo obase=16 ;" . str . " \| bc -l " . preload)
let answer = "0x" . tolower ( answer )
endif
endif
" strip newline and \
let answer = substitute (answer, "[\\\n]", "", "g")
" strip trailing 0s in decimals
let answer = substitute (answer, '\.\(\d*[1-9]\)0\+$', '.\1', "")
let answer = substitute (answer, '\.0\+$', '', "")
return answer
endfunction
" ---------------------------------------------------------------------
" CalcLines:
"
" take expression from lines, either visually selected or the current line,
" pass to calculate function, echo or past answer after '='
function! CalcLines(flag)
let has_equal = 0
" remove newlines and trailing spaces
let @e = substitute (@e, "\n", "", "g")
let @e = substitute (@e, '\s*$', "", "g")
" if we end with an equal, strip, and remember for output
if @e =~ "=$"
let @e = substitute (@e, '=$', "", "")
let has_equal = 1
endif
" if there is another equal in the line, assume chained equations, remove leading ones
let @e = substitute (@e, '^.\+=', '', '')
let answer = Calculate (@e,a:flag)
" append answer or echo
if has_equal == 1
exec "normal a " . answer
else
exec "normal a " . "= " . answer
"" echo "answer = " . answer
endif
endfunction
" ---------------------------------------------------------------------
" Check_hex:
"
" Check if the string contains 0x, a, b, c, d, e, f return has_hex=1
function! Check_hex(str)
let has_hex = 0
let ss = a:str
let ss = tolower ( ss )
if ss =~ "0x"
let has_hex = 1
return has_hex
endif
if ss =~ "[abcdef]"
let has_hex = 1
return has_hex
endif
endfunction
--//前一段时间改写vim bccalc_win插件,发现使用\16x计算取模时存在问题,计算错误。
--//建议不采用bc -l模式计算,采用bc模式计算.
--//测试如下:
10210a5e = 270600798
270600798 % 131072 %7+1 = 4 ===> 使用bc计算 \c0
10210a5e % 20000 % 7 +1 = 0x4 ===> 使用bc计算 \16x
--//以下计算错误:
10210a5e % 20000 % 7 +1 = 0x1 ===> 使用bc -l计算 \16x
270600798 % 131072 %7+1 = 1 ===> 使用bc -l计算 \cc
10210a5e % 20000 % 7 +1 = 1 ===> 使用bc -l计算 \10
--//顺便加入更多的注解,避免以后自己都读不懂源代码。在读代码时,我还发现源作者有一个插入模式计算方式,
--//实际上以前blog的上也有说明:
inoremap =: = <Esc>"eyy:call CalcLines(0)<CR>a
--//在输入表达式时输入=:就可以知道结果,当然输入要快。例子:"
12*4.2= 50.4
--//如果无法计算,显示如下,这种模式自己不太喜欢,仅仅加入=- (10to16,16to10的转换),其他不作扩展了。
123123a= (standard_in) 1: parse error
--//如果输入太慢就是如下,另外注意set paste问题,这样在插入模式无效。
12*4.2=:
--//另外为了代码的通用性修改加入如下:
if has("windows")
let str = substitute (str, '\^', '^^^^', "g")
endif
--//windows的命令行有一些变态,必须输入4个^才表示1个^.
D:\>echo 2^5 | bc
25
D:\>echo 2^^^^5 | bc
32
--//另外取消了提示行显示结果的需求,结果直接输出并且自动判断是否在结尾输入=.没有输入自动补上,整个改动有点大。
" append answer or echo
if has_equal == 1
exec "normal a " . answer
else
exec "normal a " . "= " . answer
"" echo "answer = " . answer
endif
--//源代码如下,bc.exe可执行程序自己要另行安装,一般我选择unxutils包.另外我测试仅仅<Leader>开头的命令。
$ cat vim bccalc_win.vim
"" calculate expression entered on command line and give answer, e.g.:
"" :Calculate sin (3) + sin (4) ^ 2
command! -nargs=+ Calculate echo "<args> = " . Calculate ("<args>",0)
"" calculate expression from selection using bc -l, pick a mapping, or use the Leader form
vnoremap ;bc "ey`>:call CalcLines(0)<CR>
vnoremap ;bb "ey`>:call CalcLines(0)<CR>
"" convert hexdecimal to decimal
vnoremap ;10 "ey`>:call CalcLines(10)<CR>
"" convert hexdecimal to decimal
vnoremap ;16 "ey`>:call CalcLines(16)<CR>
"" computer hexdecimal to hexdecimal
vnoremap ;16x "ey`>:call CalcLines(1016)<CR>
"" split event P1 to TYPE and MODE
vnoremap ;tx "ey`>:call CalcLines(1616)<CR>
"" split dba(10) or dba(16) to file# and block#
vnoremap ;22 "ey`>:call CalcLines(22)<CR>
vnoremap ;dba "ey`>:call CalcLines(22)<CR>
"" split scn(10) or scn(16) into scn_wrap,scn_base
vnoremap ;32 "ey`>:call CalcLines(32)<CR>
vnoremap ;scn "ey`>:call CalcLines(32)<CR>
"" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
vnoremap ;ss "ey`>:call CalcLines(10016)<CR>
"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
vnoremap ;rr "ey`>:call CalcLines(20016)<CR>
"" convert hexdecimal to decimal or decimal to hexdecimal
vnoremap ;hd "ey`>:call CalcLines(30016)<CR>
vnoremap ;hh "ey`>:call CalcLines(30016)<CR>
vnoremap ;dh "ey`>:call CalcLines(30016)<CR>
"" --------------------------------------------------------------------
"" calculate expression on current line using bc -l, pick a mapping, or use the Leader
nnoremap <Leader>bx <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap <Leader>bc <Esc>"eyy$:call CalcLines(0)<CR>
nnoremap <Leader>bb <Esc>"eyy$:call CalcLines(0)<CR>
noremap <Leader>cc Yp!!bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J
"" calculate expression on current line using bc , pick a mapping, or use the Leader
noremap <Leader>c0 Yp!!bc -q\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J
"" calculate expression on current line ( delete ,) using bc -l, pick a mapping, or use the Leader
noremap <Leader>c, Yp!!sed "s/,//g" \|bc -lq\| tr -d '\n\\\r' \| sed -e "s/\.\([0-9]*[1-9]\)0\+$/.\1/" -e "s/\.0\+$//"<CR>kA = <ESC>J
"" convert hexdecimal to decimal
nnoremap <Leader>10 <Esc>"eyy$:call CalcLines(10)<CR>
"" convert decimal to hexdecimal
nnoremap <Leader>16 <Esc>"eyy$:call CalcLines(16)<CR>
"" computer hexdecimal to hexdecimal
nnoremap <Leader>16x <Esc>"eyy$:call CalcLines(1016)<CR>
"" split event P1 to TYPE and MODE
nnoremap <Leader>tx <Esc>"eyy$:call CalcLines(1616)<CR>
"" split dba(10) or dba(16) to file# and block#
nnoremap <Leader>22 <Esc>"eyy$:call CalcLines(22)<CR>
nnoremap <Leader>dba <Esc>"eyy$:call CalcLines(22)<CR>
"" split scn(10) or scn(16) into scn_wrap,scn_base
nnoremap <Leader>32 <Esc>"eyy$:call CalcLines(32)<CR>
nnoremap <Leader>scn <Esc>"eyy$:call CalcLines(32)<CR>
"" convert scn_wrap,scn_base(10) or scn_wrap,scn_base(16) to 10 or 16 base
nnoremap <Leader>ss <Esc>"eyy$:call CalcLines(10016)<CR>
"" convert file#,block# dba(10) or file#,block# dba(16) to 10 or 16 base
nnoremap <Leader>rr <Esc>"eyy$:call CalcLines(20016)<CR>
"" convert hexdecimal to decimal or decimal to hexdecimal
nnoremap <Leader>hd <Esc>"eyy$:call CalcLines(30016)<CR>
nnoremap <Leader>hh <Esc>"eyy$:call CalcLines(30016)<CR>
nnoremap <Leader>dh <Esc>"eyy$:call CalcLines(30016)<CR>
"" --------------------------------------------------------------------
"" calculate from insertmode
inoremap =: =<Esc>"eyy$:call CalcLines(0)<CR>a
inoremap =- =<Esc>"eyy$:call CalcLines(30016)<CR>a
"" --------------------------------------------------------------------
"" Calculate:
"" clean up an expression, pass it to bc, return answer
function! Calculate (s,flag)
let has_hex = 0
let str = a:s
" remove newlines and trailing spaces
let str = substitute (str, "\n", "", "g")
let str = substitute (str, '\s*$', "", "g")
" sub common func names for bc equivalent
let str = substitute (str, '\csin\s*(', 's (', 'g')
let str = substitute (str, '\ccos\s*(', 'c (', 'g')
let str = substitute (str, '\catan\s*(', 'a (', 'g')
let str = substitute (str, "\cln\s*(", 'l (', 'g')
let str = substitute (str, '\clog\s*(', 'l (', 'g')
let str = substitute (str, '\cexp\s*(', 'e (', 'g')
" alternate exponitiation symbols
let str = substitute (str, '\*\*', '^', "g")
let str = substitute (str, '`', '^', "g")
if has("windows")
let str = substitute (str, '\^', '^^^^', "g")
endif
" escape chars for shell
if has("unix")
let str = escape (str, '*();&><|^')
endif
let preload = exists ("g:bccalc_preload") ? g:bccalc_preload : ""
" run bc
" return str
" let answer = system ("echo " . str . " \| bc -l " . preload)
if a:flag == 0
let answer = system ("echo " . str . " \| bc -l " . preload)
" let answer = answer . " --- ". str
endif
if a:flag == 10
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
let answer = system ("echo ibase=16 ;" . str . " \| bc -l " . preload)
endif
if a:flag == 16
let answer = system ("echo obase=16 ;" . str . " \| bc -l " . preload)
let answer = "0x" . tolower ( answer )
endif
if a:flag == 1016
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
let answer = system ("echo obase=16 ;ibase =16;" . str . " \| bc " . preload)
let answer = "0x" . tolower ( answer )
endif
let has_hex = Check_hex(str)
if a:flag == 1616
if has_hex == 1
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
" 0x10000 hexdecimal = 65536 (10) = 2^16(10)
let answer = system ("echo ibase=16 ;" . str . "/10000" . " \| bc " . preload)
let answer1 = system ("echo ibase=16 ;" . str . "%10000" . " \| bc " . preload)
let answer2 = system ("echo ibase=16 ;" . str . " \| bc -l " . preload)
else
let answer = system ("echo " . str . "/65536" . " \| bc " . preload)
let answer1 = system ("echo " . str . "%65536" . " \| bc " . preload)
let answer2 = "0x" . system ("echo obase=16 ;" . str . " \| bc -l " . preload)
endif
let answer = "/2^16 %2^16 (Type | Mode) = " . answer . "," . answer1 ." = " . tolower(answer2)
endif
if a:flag == 22
if has_hex == 1
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
" 0x400000 hexdecimal = 4194304 (10) = 2^22(10)
let answer = system ("echo ibase=16 ;" . str . "/400000" . " \| bc " . preload)
let answer1 = system ("echo ibase=16 ;" . str . "%400000" . " \| bc " . preload)
let answer2 = system ("echo ibase=16 ;" . str . " \| bc -l " . preload)
else
let answer = system ("echo " . str . "/4194304" . " \| bc " . preload)
let answer1 = system ("echo " . str . "%4194304" . " \| bc " . preload)
let answer2 = "0x" . system ("echo obase=16 ;" . str . " \| bc -l " . preload)
endif
" let answer = "set dba " . answer . "," . answer1
let answer = "set dba " . answer . "," . answer1 ." = alter system dump datafile " . answer . " block " . answer1 ." = " . tolower(answer2)
endif
if a:flag == 32
if has_hex == 1
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
" 0x100000000 hexdecimal = 4294967296(10) = 2^32(10)
let answer = system ("echo ibase=16 ;" . str . "/100000000" . " \| bc " . preload)
let answer1 = system ("echo ibase=16 ;" . str . "%100000000" . " \| bc " . preload)
let answer2 = system ("echo obase=16 ;ibase=16 ;" . str . "/100000000" . " \| bc " . preload)
let answer3 = system ("echo obase=16 ;ibase=16 ;" . str . "%100000000" . " \| bc " . preload)
else
let answer = system ("echo " . str . "/4294967296" . " \| bc " . preload)
let answer1 = system ("echo " . str . "%4294967296" . " \| bc " . preload)
let answer2 = system ("echo obase=16 ;" . str . "/4294967296" . " \| bc " . preload)
let answer3 = system ("echo obase=16 ;" . str . "%4294967296" . " \| bc " . preload)
endif
let answer = "scn_wrap,scn_base(10): " . answer . "," . answer1 . " = scn_wrap,scn_base(16): " . "0x" . tolower (answer2) . "," . "0x" . tolower(answer3)
endif
if a:flag == 10016
if has_hex == 1
let str = toupper (str)
let str = substitute (str, "0x", "", "g")
" 0x100000000 hexdecimal = 4294967296(10) = 2^32(10)
let str = substitute (str, "[,.]", "*100000000+", "g")
let answer = system ("echo obase=10 ;ibase=16 ;" . str . " \| bc -l " . preload)
let answer1 = system ("echo obase=16 ;ibase=16 ;" . str . " \| bc -l " . preload)
else
let str = substitute (str, "[,.]", "*4294967296+", "g")
let answer = system ("echo " . str . " \| bc -l " . preload)
let answer1 = system ("echo obase=16 ;" . str . " \| bc -l " . preload)
endif
let answer = "scn(10): " . answer . " = scn(16): " . "0x" . tolower (answer1)
endif
if a:flag == 20016
if has_hex == 1
let str = toupper ( str )
let str = substitute (str, "0x", "", "g")
" 0x400000 hexdecimal = 4194304 (10) = 2^22(10)
let str = substitute (str, "[,.]", "*400000+", "g")
let answer = system ("echo obase=10 ;ibase=16 ;" . str . " \| bc -l " . preload)
let answer1 = system ("echo obase=16 ;ibase=16 ;" . str . " \| bc -l " . preload)
else
let str = substitute (str, "[,.]", "*4194304+", "g")
let answer = system ("echo " . str . " \| bc -l " . preload)
let answer1 = system ("echo obase=16 ;" . str . " \| bc -l " . preload)
endif
let answer = "file#,block# dba(10): " . answer . " = file#,block# dba(16): " . "0x" . tolower (answer1)
endif
if a:flag == 30016
if has_hex == 1
let str = substitute (str, "0x", "", "g")
let str = toupper ( str )
let answer = system ("echo ibase=16 ;" . str . " \| bc -l " . preload)
else
let answer = system ("echo obase=16 ;" . str . " \| bc -l " . preload)
let answer = "0x" . tolower ( answer )
endif
endif
" strip newline and \
let answer = substitute (answer, "[\\\n]", "", "g")
" strip trailing 0s in decimals
let answer = substitute (answer, '\.\(\d*[1-9]\)0\+$', '.\1', "")
let answer = substitute (answer, '\.0\+$', '', "")
return answer
endfunction
" ---------------------------------------------------------------------
" CalcLines:
"
" take expression from lines, either visually selected or the current line,
" pass to calculate function, echo or past answer after '='
function! CalcLines(flag)
let has_equal = 0
" remove newlines and trailing spaces
let @e = substitute (@e, "\n", "", "g")
let @e = substitute (@e, '\s*$', "", "g")
" if we end with an equal, strip, and remember for output
if @e =~ "=$"
let @e = substitute (@e, '=$', "", "")
let has_equal = 1
endif
" if there is another equal in the line, assume chained equations, remove leading ones
let @e = substitute (@e, '^.\+=', '', '')
let answer = Calculate (@e,a:flag)
" append answer or echo
if has_equal == 1
exec "normal a " . answer
else
exec "normal a " . "= " . answer
"" echo "answer = " . answer
endif
endfunction
" ---------------------------------------------------------------------
" Check_hex:
"
" Check if the string contains 0x, a, b, c, d, e, f return has_hex=1
function! Check_hex(str)
let has_hex = 0
let ss = a:str
let ss = tolower ( ss )
if ss =~ "0x"
let has_hex = 1
return has_hex
endif
if ss =~ "[abcdef]"
let has_hex = 1
return has_hex
endif
endfunction
出处:https://www.cnblogs.com/lfree/p/15131139.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式