Advertisement
incinirate

Bases

Jul 17th, 2014
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.46 KB | None | 0 0
  1. local floor,insert = math.floor, table.insert
  2. function basen(n,b)
  3.     n = floor(n)
  4.     if not b or b == 10 then return tostring(n) end
  5.     local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  6.     local t = {}
  7.     local sign = ""
  8.     if n < 0 then
  9.         sign = "-"
  10.     n = -n
  11.     end
  12.     repeat
  13.         local d = (n % b) + 1
  14.         n = floor(n / b)
  15.         insert(t, 1, digits:sub(d,d))
  16.     until n == 0
  17.     return sign .. table.concat(t,"")
  18. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement