+ return table.concat(t) \r
+ end,\r
+ \r
+ ConvertAsciiToHex = function(s) \r
+ if s == nil then return '' end\r
+ if #s == 0 then return '' end\r
+ local t={}\r
+ for k in s:gmatch"(.)" do\r
+ table.insert(t, string.format("%02X", string.byte(k)))\r
+ end\r
+ return table.concat(t)\r
+ end,\r
+ \r
+ Chars2num = function(s)\r
+ return (s:byte(1)*16777216)+(s:byte(2)*65536)+(s:byte(3)*256)+(s:byte(4))\r
+ end,\r
+ \r
+ -- use length of string to determine 8,16,32,64 bits\r
+ bytes_to_int = function(str,endian,signed) \r
+ local t={str:byte(1,-1)}\r
+ if endian=="big" then --reverse bytes\r
+ local tt={}\r
+ for k=1,#t do\r
+ tt[#t-k+1]=t[k]\r
+ end\r
+ t=tt\r
+ end\r
+ local n=0\r
+ for k=1,#t do\r
+ n=n+t[k]*2^((k-1)*8)\r
+ end\r
+ if signed then\r
+ n = (n > 2^(#t*8-1) -1) and (n - 2^(#t*8)) or n -- if last bit set, negative.\r
+ end\r
+ return n\r