# HG changeset patch # User Matthew Wild # Date 1409677002 -3600 # Node ID 76d8907d5301a2cdcdf62a71ec017acca59a57dd # Parent f1dd1716aa9dbc607fcc598a884acf581ebb4e56 util.hex: Small util lib for converting to/from hex strings diff -r f1dd1716aa9d -r 76d8907d5301 util/hex.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/util/hex.lua Tue Sep 02 17:56:42 2014 +0100 @@ -0,0 +1,19 @@ +local s_char = string.char; + +local function char_to_hex(c) + return ("%02x"):format(c:byte()) +end + +local function hex_to_char(h) + return s_char(tonumber(h, 16)); +end + +function to(s) + return s:gsub(".", char_to_hex); +end + +function from(s) + return s:gsub("..", hex_to_char); +end + +return { to = to, from = from }