Nmap Development mailing list archives

[Lua snippit] print_hex


From: Ron <ron () skullsecurity net>
Date: Fri, 05 Sep 2008 09:19:17 -0500

Hey,

I'm so accustomed to the way Wireshark and other stuff outputs hex that I threw something together in Lua that'll do the same. It's purely for debugging my own code, not intended to be displayed to end users.

(Also, feel free to reply telling me that there's a built in function and I just waited a half hour porting 0-based arrays to 1-based arrays :) )


-- Print out a string in hex, for debugging. 
function print_hex(str)

        -- Prints out the full lines
        for line=1, string.len(str)/16, 1 do
                io.write(string.format("%08x ", (line - 1) * 16))

                -- Loop through the string, printing the hex
                for char=1, 16, 1 do
                        ch = string.byte(str, ((line - 1) * 16) + char)
                        io.write(string.format("%02x ", ch))
                end

                io.write("   ")

                -- Loop through the string again, this time the ascii
                for char=1, 16, 1 do
                        ch = string.byte(str, ((line - 1) * 16) + char)
                        if ch < 0x20 or ch > 0x7f then
                                ch = string.byte(".", 1)
                        end
                        io.write(string.format("%c", ch))
                end

                io.write("\n")
        end

        -- Prints out the final, partial line
        line = math.floor((string.len(str)/16)) + 1
        io.write(string.format("%08x ", (line - 1) * 16))

        for char=1, string.len(str) % 16, 1 do
                ch = string.byte(str, ((line - 1) * 16) + char)
                io.write(string.format("%02x ", ch))
        end
        io.write(string.rep("   ", 16 - (string.len(str) % 16)));
        io.write("   ")

        for char=1, string.len(str) % 16, 1 do
                ch = string.byte(str, ((line - 1) * 16) + char)
                if ch < 0x20 or ch > 0x7f then
                        ch = string.byte(".", 1)
                end
                io.write(string.format("%c", ch))
        end

        -- Print out the length
        io.write(string.format("\n         Length: %d [0x%x]\n", string.len(str), string.len(str)))

end


_______________________________________________
Sent through the nmap-dev mailing list
http://cgi.insecure.org/mailman/listinfo/nmap-dev
Archived at http://SecLists.Org

Current thread: