util/hex.lua

changeset 9
7da8b3c95bb1
equal deleted inserted replaced
8:2e4c32c4fb6b 9:7da8b3c95bb1
1 local s_char = string.char;
2 local s_format = string.format;
3 local s_gsub = string.gsub;
4 local s_lower = string.lower;
5
6 local char_to_hex = {};
7 local hex_to_char = {};
8
9 do
10 local char, hex;
11 for i = 0,255 do
12 char, hex = s_char(i), s_format("%02x", i);
13 char_to_hex[char] = hex;
14 hex_to_char[hex] = char;
15 end
16 end
17
18 local function to(s)
19 return (s_gsub(s, ".", char_to_hex));
20 end
21
22 local function from(s)
23 return (s_gsub(s_lower(s), "%X*(%x%x)%X*", hex_to_char));
24 end
25
26 return { to = to, from = from }

mercurial