2015-11-03 155 views
0

我使用C来使用,但是对于Lua来说是新的。有没有办法创建一个可以读取example.exe并给我十六进制程序代码的lua程序?是否有可能在Lua代码中执行hexdump

+0

'io.open( '的file.exe', 'RB')' ? – hjpotter92

+0

@ hjpotter92从某种意义上说,它与ifstream命令类似吗? –

+0

是.............. – hjpotter92

回答

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

另一种可能性:

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 
+0

'xd.lua'是在引入通用'for'和'gmatch'之前编写的。感谢这个现代版本的'xd.lua'。 – lhf

相关问题