Lua base converter
I need a base converter function for Lu开发者_开发技巧a. 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?
In the string
to number
direction, the function tonumber()
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,"") end
This 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 while table.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 t
with calls to table.insert(t,1,digit)
, or to append them to the end with t[#t+1]=digit
, followed by a call to string.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 for string.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 n
to an integer with math.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.
you can use a loop to convert an integer into a string containting the required base. for bases below 10 use the following code, if you need a base larger than that you need to add a line that mapps the result of x % base to a character (usign an array for example)
x = 1234
r = ""
base = 8
while x > 0 do
r = "" .. (x % base ) .. r
x = math.floor(x / base)
end
print( r );
精彩评论