aboutsummaryrefslogtreecommitdiff
path: root/src/utils.lua
blob: 5851e1c94868c7ab8006d179d31f1a0b98f8b51c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
--
-- ~~~ helper utils
--

-- string -> hex
function string.tohex(str)
    return (str:gsub(".", function (c)
        return string.format("%02X", string.byte(c))
    end))
end

-- hex -> string
function string.fromhex(str)
    return (str:gsub("..", function (cc)
        return string.char(tonumber(cc, 16))
    end))
end

-- table[start, end] -> hex
function table.extracthex(response, start, last)

    local result = {}

    table.move(response, start, last, 1, result)
    result = string.tohex(table.concat(result))

    return result

end

-- table -> pretty print
function table.dump(response)

    if type(response) == "table" then

        local start = "{ "

        for key, value in pairs(response) do

            if type(key) ~= "number" then key = '"' .. key .. '"' end

            start = start .. "[" .. key .. "] = " .. table.dump(value) .. ","

        end

        return start .. "} "

    else

        return tostring(response)

    end
end