Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- done in LuaU, expect compound assignments and exclusive keywords
- function FACT_NumToStrFromBase(charstable)
- local base = #charstable
- return function(num)
- local res = ''
- local its = 1 + math.max(0,math.floor(math.log(num,base)))
- for i=its,1,-1 do -- going down number places
- local factor = base ^ (i - 1)
- local nk = base - 1
- for k=base,1,-1 do -- going down the base to 1
- local num2 = factor * (k - 1)
- if num - num2 < 0 then continue end
- nk = k
- num -= num2
- break
- end
- res ..= charstable[nk]
- end
- return res
- end
- end
- function FACT_StrToNumFromBase(charstable)
- local base = #charstable
- local FlippedCharacters = {}
- for i,v in next,charstable do FlippedCharacters[tostring(v)] = i end
- return function(str)
- local res = 0
- for a=1,#str do -- iterate each letter
- local letter = str:sub(a,a)
- local num = (FlippedCharacters[letter] - 1) * base ^ (#str - a)
- res += num
- end
- return res
- end
- end
- local numtobin = FACT_NumToStrFromBase(string.split('01',''))
- local numtohex = FACT_NumToStrFromBase(string.split('0123456789abcdef',''))
- local bintonum = FACT_StrToNumFromBase((string.split('01','')))
- local hextonum = FACT_StrToNumFromBase(string.split('0123456789abcdef',''))
- for a=0,32 do
- local b = numtobin(a)
- print(a,b,bintonum(b))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement