[英]Lua - ‘String to Hex’ and ‘Hex to String’ formulas
I've tried to keep various examples of Lua code I've come across to re-use, with strtohex() and hextostr() being one an example, but I can only find strToHex() - below..我试图保留我遇到的 Lua 代码的各种示例以重复使用,其中 strtohex() 和 hextostr() 就是一个示例,但我只能在下面找到 strToHex()。
local s = "175"
local function strToHex(s)
local bytes = {}
for i=1,s:len() do
bytes[#bytes+1] = ('%2x'):format(s:byte(i,i))
end
return table.concat(bytes, ' ')
--return table.concat(bytes, '')
end
print(strToHex(s))
Does anyone have a example of hextostr() they could share?有没有人可以分享一个 hextostr() 的例子?
local function hexdecode(hex)
return (hex:gsub("%x%x", function(digits) return string.char(tonumber(digits, 16)) end))
end
local function hexencode(str)
return (str:gsub(".", function(char) return string.format("%2x", char:byte()) end))
end
print(hexdecode(hexencode("Hello, World!")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.