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))
90 -- function convertStringToBytes(str)
92 -- local strLength = string.len(str)
93 -- for i=1,strLength do
94 -- table.insert(bytes, string.byte(str, i))
100 -- function convertBytesToString(bytes)
101 -- local bytesLength = table.getn(bytes)
103 -- for i=1,bytesLength do
104 -- str = str .. string.char(bytes[i])
110 -- function convertHexStringToBytes(str)
112 -- local strLength = string.len(str)
113 -- for k=2,strLength,2 do
114 -- local hexString = "0x" .. string.sub(str, (k - 1), k)
115 -- table.insert(bytes, hex.to_dec(hexString))
121 -- function convertBytesToHexString(bytes)
123 -- local bytesLength = table.getn(bytes)
124 -- for i=1,bytesLength do
125 -- local hexString = string.sub(hex.to_hex(bytes[i]), 3)
126 -- if string.len(hexString) == 1 then
127 -- hexString = "0" .. hexString
129 -- str = str .. hexString