Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
char2int & int2char encoding
--lua 5.3 local function getsymbols() Char2Int = {} Int2Char = {} local i = 0 while(i<=string.len(symbols))do Char2Int[string.sub(symbols,i,i)] = i Int2Char[i]= string.sub(symbols,i,i) i = i + 1 end ------------------------------------------------ end symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" getsymbols() local function ChangeInt2Char(int) local int = tostring(int) local maxn = string.len(symbols) local str = "" while(tonumber(int)>maxn)do local residue = tostring(math.fmod(tonumber(int),maxn)) local divizor = tostring((tonumber(int)-tonumber(residue))/maxn) print("( "..int.." - "..residue.." )/ "..maxn.." = "..divizor) str = str..Int2Char[tonumber(residue)] int = tostring(divizor) end print("======================================") str = str..Int2Char[tonumber(int)] return str end local function ChangeChar2Int(str) str = string.reverse(str) local maxn = string.len(symbols) local len = string.len(str) local int = 0 local letters = {} letters[0] = Char2Int[string.sub(str,len,-1)] str = string.sub(str,0,string.len(str)-1) local i = 1 while(string.len(str)~=0)do letters[i] = Char2Int[string.sub(str,string.len(str),-1)] str = string.sub(str,0,string.len(str)-1) i = i + 1 end local a = 1 while(a<i)do local n = tostring(maxn^a) int = tostring(tonumber(int)+letters[a]*tonumber(n)) a = a + 1 end int = int + letters[0] return tostring(int) end print("Int2Char: "..ChangeInt2Char(10^13+5)) print("Char2Int: "..ChangeChar2Int("INSJq2P1")) --precision cap is ~10^14 ???
run
|
edit
|
history
|
help
0
Volume of sphere
sh
test123
bf-asm compiler test 2
test3
VWPASSATPOLIZEI
Hello 2
cases
Fixed
Area and Circumference of a circle.