Software /
code /
prosody
Annotate
util/hex.lua @ 6539:f923140ee7c5
net.server_select: Fix timers not being fired until another timer fixes (or 1 second passes)
author | daurnimator <quae@daurnimator.com> |
---|---|
date | Thu, 15 Jan 2015 09:03:00 -0500 |
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 } |