Comparison

util/hex.lua @ 6375:76d8907d5301

util.hex: Small util lib for converting to/from hex strings
author Matthew Wild <mwild1@gmail.com>
date Tue, 02 Sep 2014 17:56:42 +0100
child 6384:3f4809d01783
comparison
equal deleted inserted replaced
6374:f1dd1716aa9d 6375:76d8907d5301
1 local s_char = string.char;
2
3 local function char_to_hex(c)
4 return ("%02x"):format(c:byte())
5 end
6
7 local function hex_to_char(h)
8 return s_char(tonumber(h, 16));
9 end
10
11 function to(s)
12 return s:gsub(".", char_to_hex);
13 end
14
15 function from(s)
16 return s:gsub("..", hex_to_char);
17 end
18
19 return { to = to, from = from }