我习惯使用C但是对Lua来说是新手。有没有办法创建一个lua程序,可以读取example.exe并给我十六进制的程序代码?
答案 0 :(得分:3)
在Lua 5.1之前,此示例程序xd.lua
包含在发行版中:
-- hex dump
-- usage: lua xd.lua < file
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
答案 1 :(得分:1)
另一种可能性:
local filename = arg[1]
if filename == nil then
print [=[
Usage: 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