在Lua 4中将十进制转换为十六进制? [英] Convert decimal to hex in Lua 4?
问题描述
我发现此公式可将Lua中的十进制数字转换为十六进制颜色值:
I found this formula to convert decimal numbers to hexadecimal color values in Lua:
http://lua-users.org/lists/lua-l/2004-09/msg00054.html
但是,我对公式有一些疑问:
However, I have a few questions about the formula:
- 我的输入需要在0到1之间而不是0到255之间进行标准化.这是问题吗?
- 我坚持使用Lua 4.01,而不是最新版本.我无法升级.这有问题吗?
谢谢!
推荐答案
在Lua 5.x中,可以将string.format函数与%x
格式说明符一起使用,以将整数转换为十六进制表示形式.在您的情况下,它看起来像这样:
In Lua 5.x you can use the string.format function with the %x
format specifier to convert integers to their hexadecimal representation. In your case it would look like this:
local input = 0.5
local output = string.format("%x", input * 256) -- "80"
我不太了解Lua 4.0.1,因此我无法告诉您此功能是否可用(也许使用其他名称).也就是说,如果不是,那么您可以通过将其转换为使用sscanf
的C函数来解决.
I don't know Lua 4.0.1 well so I can't tell you if this function is available (perhaps under a different name). That said, if its not, then you might be able to workaround by turning this into a C function that uses sscanf
.
这篇关于在Lua 4中将十进制转换为十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!