Rob*_*Rob 2 string formatting lua padding
我有这个:
function dec2hex(IN)
local OUT
OUT = string.format("%x",IN)
return OUT
end
Run Code Online (Sandbox Code Playgroud)
并且需要IN将零填充到字符串长度 6。
我无法使用String.Utils或PadLeft。它位于一个名为 Watchmaker 的应用程序中,该应用程序使用 Lua 的精简版本。
Lua 中的字符串格式与 C 中的工作方式基本相同。因此,要用零填充数字,只需使用%0nwheren是位数即可。例如
print(string.format("%06x", 16^4-1))\nRun Code Online (Sandbox Code Playgroud)\n\n将打印00ffff.
请参阅第 20 章Lua\xe2\x80\x9d 中的 \xe2\x80\x9c 编程的字符串库string.format、 的参考以及该printf系列的 C 参考详细内容