Created
May 14, 2019 08:06
-
-
Save tsuyoshicho/192596028867096ab7469cb318c0af78 to your computer and use it in GitHub Desktop.
Base36 test vim
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
| " base36 | |
| " byte list | |
| function! s:base36encode(data) | |
| let data = a:data | |
| let base36code = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
| let base36codelist = split(base36code, '.\zs') | |
| let retval = '' | |
| for i in data | |
| let buffer = [] | |
| let value = i | |
| while value > 0 | |
| let index = value % 36 | |
| let value = value / 36 | |
| let buffer = [base36codelist[index]] + buffer | |
| endwhile | |
| " debug | |
| echomsg string(buffer) | |
| let retval = retval . join(buffer, '') | |
| endfor | |
| return retval | |
| endfunction | |
| function! s:_binstr2bytes(str) abort | |
| return map(range(len(a:str)/2), 'str2nr(a:str[v:val*2 : v:val*2+1], 16)') | |
| endfunction | |
| function! s:_str2bytes(str) abort | |
| return map(range(len(a:str)), 'char2nr(a:str[v:val])') | |
| endfunction | |
| function! s:_bytes2str(bytes) abort | |
| return eval('"' . join(map(copy(a:bytes), 'printf(''\x%02x'', v:val)'), '') . '"') | |
| endfunction | |
| echomsg s:base36encode(s:_str2bytes('test')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment