Created
August 28, 2013 03:37
-
-
Save Elemecca/6361899 to your computer and use it in GitHub Desktop.
Lua function which creates a hex dump of a binary string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
There's a slight bug with this when the data is exactly aligned to 8 bytes: