用零填充十六进制字符串以达到 6 个字符的长度

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.UtilsPadLeft。它位于一个名为 Watchmaker 的应用程序中,该应用程序使用 Lua 的精简版本。

Hen*_*nke 5

Lua 中的字符串格式与 C 中的工作方式基本相同。因此,要用零填充数字,只需使用%0nwheren是位数即可。例如

\n\n
print(string.format("%06x", 16^4-1))\n
Run Code Online (Sandbox Code Playgroud)\n\n

将打印00ffff.

\n\n

请参阅第 20 章Lua\xe2\x80\x9d 中的 \xe2\x80\x9c 编程的字符串库string.format、 的参考以及该printf系列的 C 参考详细内容

\n