Lua基础转换器 [英] Lua base converter
问题描述
我需要Lua的基本转换器功能.我需要将以10为基数的基数转换为以2,3,4,5,6,7,8,9,10,11 ... 36为基数的数字?
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?
推荐答案
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.
在数字到字符串的方向上,这可以比尼古劳斯的答案效率更高. a>像这样:
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
这将减少使用table.concat()
收集的垃圾字符串,而不是重复调用字符串串联运算符..
.尽管对于这么小的字符串几乎没有实际的区别,但是应该学习这种习惯用法,因为否则使用级联运算符在循环中构建缓冲区实际上会趋向于提高O(n 2 )的性能,而table.concat()
具有被设计成可以做得更好.
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.
对于通过调用table.insert(t,1,digit)
将表中的数字压入表t
还是将其附加到末尾并添加t[#t+1]=digit
,是否更有效?调用string.reverse()
将数字按正确的顺序排列.我将基准测试留给学生.请注意,尽管我粘贴到此处的代码确实可以运行并且看起来可以得到正确的答案,但是还有其他机会可以对其进行进一步的调整.
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.
例如,基座10的常见情况是通过内置的tostring()
函数剔除并处理的.但是,对于具有8位和16位(分别具有string.format()
(分别为"%o"
和"%x"
)的转换说明符的)基数可以进行类似的剔除.
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).
此外,尼古拉斯的解决方案和我的解决方案都无法很好地处理非整数问题.在这里,我通过将值n
强制为以math.floor()
开头的整数来强调这一点.
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.
正确地将通用浮点值转换为任何基数(甚至是基数10)充满了微妙之处,我留给读者练习.
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.
这篇关于Lua基础转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!