Reputation: 1
I'm used to using C but I'm new to Lua. Is there a way to create a Lua program that can read example.exe
and give me the program's code in hexadecimal?
Upvotes: 0
Views: 1086
Reputation: 3225
Another possibility:
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
Upvotes: 2
Reputation: 72352
Until Lua 5.1, this sample program xd.lua
was included in the distribution:
-- 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
Upvotes: 4