]>
Commit | Line | Data |
---|---|---|
5b1311fb MHS |
1 | --[[\r |
2 | This may be moved to a separate library at some point (Holiman)\r | |
3 | --]]\r | |
4 | local Utils = \r | |
5 | {\r | |
6 | -- Asks the user for Yes or No\r | |
7 | confirm = function(message, ...)\r | |
8 | local answer\r | |
9 | message = message .. " [y/n] ?"\r | |
10 | repeat\r | |
11 | io.write(message)\r | |
12 | io.flush()\r | |
13 | answer=io.read()\r | |
14 | if answer == 'Y' or answer == "y" then\r | |
15 | return true\r | |
16 | elseif answer == 'N' or answer == 'n' then \r | |
17 | return false\r | |
18 | end\r | |
19 | until false\r | |
20 | end,\r | |
21 | ---\r | |
22 | -- Asks the user for input\r | |
23 | input = function (message , default)\r | |
24 | local answer\r | |
25 | if default ~= nil then\r | |
26 | message = message .. " (default: ".. default.. " )"\r | |
27 | end\r | |
28 | message = message .." \n > "\r | |
29 | io.write(message)\r | |
30 | io.flush()\r | |
31 | answer=io.read()\r | |
32 | if answer == '' then answer = default end\r | |
33 | \r | |
34 | return answer\r | |
35 | end,\r | |
36 | --\r | |
37 | -- Converts DECIMAL to HEX\r | |
38 | ConvertDec2Hex = function(IN)\r | |
39 | local B,K,OUT,I,D=16,"0123456789ABCDEF","",0\r | |
40 | while IN>0 do\r | |
41 | I=I+1\r | |
42 | IN,D=math.floor(IN/B),math.mod(IN,B)+1\r | |
43 | OUT=string.sub(K,D,D)..OUT\r | |
44 | end\r | |
45 | return OUT\r | |
46 | end,\r | |
47 | ---\r | |
48 | -- Convert Byte array to string of hex\r | |
cd5767d4 | 49 | ConvertBytes2HexString = function(bytes)\r |
50 | if #bytes == 0 then\r | |
51 | return ''\r | |
52 | end\r | |
53 | local s={}\r | |
5b1311fb | 54 | for i = 1, #(bytes) do\r |
9b989c45 | 55 | s[i] = string.format("%02X",bytes[i]) \r |
5b1311fb MHS |
56 | end\r |
57 | return table.concat(s)\r | |
58 | end, \r | |
cd5767d4 | 59 | -- Convert byte array to string with ascii\r |
60 | ConvertBytesToAsciiString = function(bytes)\r | |
61 | if #bytes == 0 then\r | |
62 | return ''\r | |
63 | end\r | |
64 | local s={}\r | |
65 | for i = 1, #(bytes) do\r | |
66 | s[i] = string.char(bytes[i]) \r | |
67 | end\r | |
68 | return table.concat(s) \r | |
69 | end, \r | |
70 | ConvertHexStringToBytes = function(s)\r | |
9b989c45 | 71 | local t={}\r |
cd5767d4 | 72 | if s == nil then return t end\r |
73 | if #s == 0 then return t end\r | |
9b989c45 | 74 | for k in s:gmatch"(%x%x)" do\r |
75 | table.insert(t,tonumber(k,16))\r | |
76 | end\r | |
77 | return t\r | |
78 | end,\r | |
cd5767d4 | 79 | ConvertAsciiStringToBytes = function(s)\r |
80 | local t={}\r | |
81 | if s == nil then return t end\r | |
82 | if #s == 0 then return t end\r | |
83 | \r | |
84 | for k in s:gmatch"(.)" do\r | |
85 | table.insert(t, string.byte(k))\r | |
86 | end\r | |
87 | return t\r | |
88 | end,\r | |
47cbb2d4 | 89 | ConvertHexToAscii = function(s)\r |
90 | local t={}\r | |
91 | if s == nil then return t end\r | |
92 | if #s == 0 then return t end\r | |
93 | for k in s:gmatch"(%x%x)" do\r | |
94 | table.insert(t, string.char(tonumber(k,16)))\r | |
95 | end\r | |
96 | return table.concat(t) \r | |
97 | end,\r | |
9b989c45 | 98 | \r |
99 | -- function convertStringToBytes(str)\r | |
100 | -- local bytes = {}\r | |
101 | -- local strLength = string.len(str)\r | |
102 | -- for i=1,strLength do\r | |
103 | -- table.insert(bytes, string.byte(str, i))\r | |
104 | -- end\r | |
105 | \r | |
106 | -- return bytes\r | |
107 | -- end\r | |
108 | \r | |
109 | -- function convertBytesToString(bytes)\r | |
110 | -- local bytesLength = table.getn(bytes)\r | |
111 | -- local str = ""\r | |
112 | -- for i=1,bytesLength do\r | |
113 | -- str = str .. string.char(bytes[i])\r | |
114 | -- end\r | |
115 | \r | |
116 | -- return str\r | |
117 | -- end\r | |
118 | \r | |
119 | -- function convertHexStringToBytes(str)\r | |
120 | -- local bytes = {}\r | |
121 | -- local strLength = string.len(str)\r | |
122 | -- for k=2,strLength,2 do\r | |
123 | -- local hexString = "0x" .. string.sub(str, (k - 1), k)\r | |
124 | -- table.insert(bytes, hex.to_dec(hexString))\r | |
125 | -- end\r | |
126 | \r | |
127 | -- return bytes\r | |
128 | -- end\r | |
129 | \r | |
130 | -- function convertBytesToHexString(bytes)\r | |
131 | -- local str = ""\r | |
132 | -- local bytesLength = table.getn(bytes)\r | |
133 | -- for i=1,bytesLength do\r | |
134 | -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)\r | |
135 | -- if string.len(hexString) == 1 then\r | |
136 | -- hexString = "0" .. hexString\r | |
137 | -- end\r | |
138 | -- str = str .. hexString\r | |
139 | -- end\r | |
140 | \r | |
141 | -- return str\r | |
142 | -- end\r | |
143 | \r | |
5b1311fb MHS |
144 | }\r |
145 | return Utils |