I need a base converter function for Lua. I need to convert from base 10 to base 2,3,4,5,6,7,8,9,10,11…36 how can i to this?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In the
stringtonumberdirection, the functiontonumber()takes an optional second argument that specifies the base to use, which may range from 2 to 36 with the obvious meaning for digits in bases greater than 10.In the number to string direction, this can be done slightly more efficiently than Nikolaus’s answer by something like this:
local floor,insert = math.floor, table.insert function basen(n,b) n = floor(n) if not b or b == 10 then return tostring(n) end local digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" 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,"") endThis creates fewer garbage strings to collect by using
table.concat()instead of repeated calls to the string concatenation operator... Although it makes little practical difference for strings this small, this idiom should be learned because otherwise building a buffer in a loop with the concatenation operator will actually tend to O(n2) performance whiletable.concat()has been designed to do substantially better.There is an unanswered question as to whether it is more efficient to push the digits on a stack in the table
twith calls totable.insert(t,1,digit), or to append them to the end witht[#t+1]=digit, followed by a call tostring.reverse()to put the digits in the right order. I’ll leave the benchmarking to the student. Note that although the code I pasted here does run and appears to get correct answers, there may other opportunities to tune it further.For example, the common case of base 10 is culled off and handled with the built in
tostring()function. But similar culls can be done for bases 8 and 16 which have conversion specifiers forstring.format()("%o"and"%x", respectively).Also, neither Nikolaus’s solution nor mine handle non-integers particularly well. I emphasize that here by forcing the value
nto an integer withmath.floor()at the beginning.Correctly converting a general floating point value to any base (even base 10) is fraught with subtleties, which I leave as an exercise to the reader.