Skip to content

Instantly share code, notes, and snippets.

@aclisp
Forked from Elemecca/hex_dump.lua
Created November 30, 2023 01:10
Show Gist options
  • Save aclisp/6c08f7cffe19ffdb764d05dd9c915243 to your computer and use it in GitHub Desktop.
Save aclisp/6c08f7cffe19ffdb764d05dd9c915243 to your computer and use it in GitHub Desktop.
Lua function which creates a hex dump of a binary string.
function hex_dump (str)
local len = string.len( str )
local dump = ""
local hex = ""
local asc = ""
for i = 1, len do
if 1 == i % 8 then
dump = dump .. hex .. asc .. "\n"
hex = string.format( "%04x: ", i - 1 )
asc = ""
end
local ord = string.byte( str, i )
hex = hex .. string.format( "%02x ", ord )
if ord >= 32 and ord <= 126 then
asc = asc .. string.char( ord )
else
asc = asc .. "."
end
end
return dump .. hex
.. string.rep( " ", 8 - len % 8 ) .. asc
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment