From 56b4c1c856f107e178aede2313d943022ad3c73d Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Tue, 2 Sep 2014 17:56:42 +0100 Subject: util.hex: Small util lib for converting to/from hex strings --- util/hex.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 util/hex.lua (limited to 'util') diff --git a/util/hex.lua b/util/hex.lua new file mode 100644 index 00000000..72fc22bd --- /dev/null +++ b/util/hex.lua @@ -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 } -- cgit v1.2.3