Created
September 5, 2015 07:19
-
-
Save rbtnn/b2477ef706b302fed4ea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function! s:str2nr(str, base) abort | |
let xs = map(range(0x30, 0x39) + range(0x41, 0x5A), 'nr2char(v:val)') | |
let n = 0 | |
let total = 0 | |
if a:base == 1 | |
return len(a:str) - 1 | |
else | |
for c in reverse(split(a:str, '\zs')) | |
let total += index(xs, c) * pow(a:base, n) | |
let n += 1 | |
endfor | |
return total | |
endif | |
endfunction | |
function! s:nr2str(n, base) abort | |
let xs = map(range(0x30, 0x39) + range(0x41, 0x5A), 'nr2char(v:val)') | |
let rest = a:n | |
let str = '' | |
while 0 < rest | |
let str = xs[rest % a:base] . str | |
let rest = rest / a:base | |
endwhile | |
return str | |
endfunction | |
echo s:str2nr('101', 2) | |
" 5.0 | |
echo s:nr2str(129, 36) | |
" 3L |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment