Lua代码中是否有可能执行十六进制转储?

我习惯使用 C,但对 Lua 还很陌生。有没有办法创建一个 Lua 程序,可以读取 example.exe 并以十六进制形式给出程序的代码?

点赞
用户107090
用户107090

直到 Lua 5.1 版本,此样例程序 xd.lua 才被包含在发行版中:

-- 十六进制转储
-- 用法:lua xd.lua < 文件

local offset=0
while true do
 local s=io.read(16)
 if s==nil then return end
 io.write(string.format("%08X  ",offset))
 string.gsub(s,"(.)",
    function (c) io.write(string.format("%02X ",string.byte(c))) end)
 io.write(string.rep(" ",3*(16-string.len(s))))
 io.write(" ",string.gsub(s,"%c","."),"\n")
 offset=offset+16
end
2015-11-03 18:39:37
用户3735873
用户3735873

另一种可能性:

local filename = arg[1]

if filename == nil then
  print [=[
使用方法:dump <filename> [bytes_per_line(16)]]=]
  return
end

local f = assert(io.open(filename, 'rb'))
local block = tonumber(arg[2]) or 16

while true do
  local bytes = f:read(block)
  if not bytes then return end
  for b in bytes:gmatch('.') do
    io.write(('%02X '):format(b:byte()))
  end
  io.write(('   '):rep(block - bytes:len() + 1))
  io.write(bytes:gsub('%c', '.'), '\n')
end
2015-11-04 17:26:22