Created
May 24, 2014 12:16
-
-
Save LennyPenny/7d2d5fb76de5ae1e5c8e to your computer and use it in GitHub Desktop.
lua base6xxx
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
local function baseEnc(n, b) | |
n = math.floor(n) | |
if not b or b == 10 then return tostring(n) end | |
local digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | |
local t = {} | |
local sign = "" | |
if n < 0 then | |
sign = "-" | |
n = -n | |
end | |
repeat | |
local d = (n % b) + 1 | |
n = math.floor(n / b) | |
table.insert(t, 1, digits:sub(d, d)) | |
until n == 0 | |
return sign..table.concat(t, "") | |
end | |
print(baseEnc(100000000, 62)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment