【问题标题】:Lua - ‘String to Hex’ and ‘Hex to String’ formulasLua - “字符串到十六进制”和“十六进制到字符串”公式
【发布时间】:2020-12-28 20:28:12
【问题描述】:

我试图保留我遇到的 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))

有没有人可以分享 hextostr() 的例子?

【问题讨论】:

    标签: lua hex


    【解决方案1】:
    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!")))
    

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 2020-11-07
      • 2016-02-26
      • 2019-07-27
      相关资源
      最近更新 更多