aboutsummaryrefslogtreecommitdiffstats
path: root/util/hex.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2014-09-02 17:56:42 +0100
committerMatthew Wild <mwild1@gmail.com>2014-09-02 17:56:42 +0100
commit56b4c1c856f107e178aede2313d943022ad3c73d (patch)
tree05f331e7be0678dbf158f8ee9d7af918cef2293f /util/hex.lua
parentc2d718c06a1a178167ccf8c2dc4b865dc41b537e (diff)
downloadprosody-56b4c1c856f107e178aede2313d943022ad3c73d.tar.gz
prosody-56b4c1c856f107e178aede2313d943022ad3c73d.zip
util.hex: Small util lib for converting to/from hex strings
Diffstat (limited to 'util/hex.lua')
-rw-r--r--util/hex.lua19
1 files changed, 19 insertions, 0 deletions
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 }