Created
November 1, 2012 22:16
-
-
Save evilpacket/3997031 to your computer and use it in GitHub Desktop.
Base N in Lua
This file contains 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 floor,insert = math.floor, table.insert | |
local function basen(n,b) | |
n = floor(n) | |
if not b or b == 10 then return tostring(n) end | |
local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
local t = {} | |
local sign = "" | |
if n < 0 then | |
sign = "-" | |
n = -n | |
end | |
repeat | |
local d = (n % b) + 1 | |
n = floor(n / b) | |
insert(t, 1, digits:sub(d,d)) | |
until n == 0 | |
return sign .. table.concat(t,"") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment