Software /
code /
prosody
Annotate
util/hex.lua @ 6479:d016437e01bf
plugins/muc/mod_muc: Add 'local_only' flag to mod_muc, so rooms don't get restored on shutdown
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Fri, 17 Oct 2014 16:35:46 -0400 |
parent | 6384:3f4809d01783 |
child | 6545:ec566d7cd518 |
rev | line source |
---|---|
6375
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local s_char = string.char; |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 local function char_to_hex(c) |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 return ("%02x"):format(c:byte()) |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 end |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 local function hex_to_char(h) |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 return s_char(tonumber(h, 16)); |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 end |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 |
6384 | 11 local function to(s) |
6375
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 return s:gsub(".", char_to_hex); |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 end |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 |
6384 | 15 local function from(s) |
6375
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 return s:gsub("..", hex_to_char); |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 end |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 |
76d8907d5301
util.hex: Small util lib for converting to/from hex strings
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 return { to = to, from = from } |