使用库函数进行转换:
在线地址:https://wiki.luatos.com/_static/luatos-emulator/lua.html
function fromHex(hex)
--滤掉分隔符
local hex = hex:gsub("[%s%p]", ""):upper()
return hex:gsub("%x%x", function(c)
return string.char(tonumber(c, 16))
end)
end
function toValue(str)
return fromHex(str:gsub("%x", "0%1"))
end
local s="123456ef"
local hexs=toValue(s)
for i=1,8 do
print(i, "ascii: ", s:byte(i), ", hex: ", hexs:byte(i))
end
执行结果:
[01:09:36] 虚拟机初始化完毕
[01:09:36] 1 ascii: 49 , hex: 1
[01:09:36] 2 ascii: 50 , hex: 2
[01:09:36] 3 ascii: 51 , hex: 3
[01:09:36] 4 ascii: 52 , hex: 4
[01:09:36] 5 ascii: 53 , hex: 5
[01:09:36] 6 ascii: 54 , hex: 6
[01:09:36] 7 ascii: 101 , hex: 14
[01:09:36] 8 ascii: 102 , hex: 15
[01:09:36] 虚拟机已停止运行