2 This may be moved to a separate library at some point (Holiman)
6 -- Asks the user for Yes or No
7 confirm = function(message, ...)
9 message = message .. " [y/n] ?"
14 if answer == 'Y' or answer == "y" then
16 elseif answer == 'N' or answer == 'n' then
22 -- Asks the user for input
23 input = function (message , default)
25 if default ~= nil then
26 message = message .. " (default: ".. default.. " )"
28 message = message .." \n > "
32 if answer == '' then answer = default end
37 -- Converts DECIMAL to HEX
38 ConvertDec2Hex = function(IN)
39 local B,K,OUT,I,D=16,"0123456789ABCDEF","",0
42 IN,D=math.floor(IN/B),math.mod(IN,B)+1
43 OUT=string.sub(K,D,D)..OUT
48 -- Convert Byte array to string of hex
49 ConvertBytes2HexString = function(bytes)
54 for i = 1, #(bytes) do
55 s[i] = string.format("%02X",bytes[i])
57 return table.concat(s)
59 -- Convert byte array to string with ascii
60 ConvertBytesToAsciiString = function(bytes)
65 for i = 1, #(bytes) do
66 s[i] = string.char(bytes[i])
68 return table.concat(s)
70 ConvertHexStringToBytes = function(s)
72 if s == nil then return t end
73 if #s == 0 then return t end
74 for k in s:gmatch"(%x%x)" do
75 table.insert(t,tonumber(k,16))
79 ConvertAsciiStringToBytes = function(s)
81 if s == nil then return t end
82 if #s == 0 then return t end
84 for k in s:gmatch"(.)" do
85 table.insert(t, string.byte(k))
89 ConvertHexToAscii = function(s)
91 if s == nil then return t end
92 if #s == 0 then return t end
93 for k in s:gmatch"(%x%x)" do
94 table.insert(t, string.char(tonumber(k,16)))
96 return table.concat(t)
99 -- function convertStringToBytes(str)
101 -- local strLength = string.len(str)
102 -- for i=1,strLength do
103 -- table.insert(bytes, string.byte(str, i))
109 -- function convertBytesToString(bytes)
110 -- local bytesLength = table.getn(bytes)
112 -- for i=1,bytesLength do
113 -- str = str .. string.char(bytes[i])
119 -- function convertHexStringToBytes(str)
121 -- local strLength = string.len(str)
122 -- for k=2,strLength,2 do
123 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
124 -- table.insert(bytes, hex.to_dec(hexString))
130 -- function convertBytesToHexString(bytes)
132 -- local bytesLength = table.getn(bytes)
133 -- for i=1,bytesLength do
134 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
135 -- if string.len(hexString) == 1 then
136 -- hexString = "0" .. hexString
138 -- str = str .. hexString