-
Notifications
You must be signed in to change notification settings - Fork 24
/
bin2hex.lua
44 lines (33 loc) · 999 Bytes
/
bin2hex.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
-- Output a file as hex for use with binrun.lua and binrundll.lua
local ffi = require("ffi")
-- Function to read a file and return its content as a hex array
local function readFileAsHexArray(filePath)
local file = io.open(filePath, "rb")
if not file then
return nil
end
local content = file:read("*all")
file:close()
local hexArray = {}
for i = 1, #content do
local byte = string.byte(content, i)
table.insert(hexArray, string.format("\\x%02X", byte))
end
return hexArray
end
-- Main function
local function main()
local filePath = arg[1]
if not filePath then
print("Usage: lua script.lua <file_path>")
return
end
local hexArray = readFileAsHexArray(filePath)
if not hexArray then
print("Failed to open or read the file.")
return
end
-- Print the hex array as a C-style array for scripts
print("local data = \"" .. table.concat(hexArray, "") .. "\";")
end
main()