Skip to content

Instantly share code, notes, and snippets.

@evilpacket
Created November 1, 2012 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evilpacket/3997031 to your computer and use it in GitHub Desktop.
Save evilpacket/3997031 to your computer and use it in GitHub Desktop.
Base N in Lua
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