-- ********************************************************************************************** -- CRC_Calculate - Calculate a 32-bit CRC based on a given polynomial and block of data. -- This function caluclates a CRC checksum. -- Arguments: -- gen_poly - The generator polynomial. (MSB-0) -- data - A table with the data to run the CRC on. -- Returns: -- The checksum function calculate_CRC(gen_poly, data) local remainder = 0 for i=1, #data do remainder = bit.bxor(remainder, bit.blshift(data[i], 24)) for i=1, 8 do if (bit.band(remainder, 0x80000000) > 0) then remainder = bit.bxor(bit.blshift(remainder,1), gen_poly) else remainder = bit.blshift(remainder,1) end end end return remainder end -- ********************************************************************************************** -- intToHex() - Convert a number to a hexadecimal string -- Arguments: -- input - The input number -- Returns: -- string output -- This function is deprecated, use string.format("%X", input) instead. (Thanks to tomass1996 for pointing this out) function intToHex(input) -- local output = {} -- local iter = 1 -- while true do -- output[iter] = input % 16 -- input = math.floor((input - output[iter]) / 16) -- if(output[iter] == 10) then -- output[iter] = "A" -- elseif(output[iter] == 11) then -- output[iter] = "B" -- elseif(output[iter] == 12) then -- output[iter] = "C" -- elseif(output[iter] == 13) then -- output[iter] = "D" -- elseif(output[iter] == 14) then -- output[iter] = "E" -- elseif(output[iter] == 15) then -- output[iter] = "F" -- end -- if ( input == 0 ) then -- return table.concat(output) -- end -- iter = iter+1 -- end return string.format("%X", input) end -- ********************************************************************************************** -- intToHex() - Convert a hexadecimal digit into a number TYPE ("string" to "number") -- Arguments: -- input - The input hex digit -- Returns: -- number output -- This function is deprecated, use tonumber(input, 16) instead function hexToInt(input) -- if (tonumber(input)) then -- return tonumber(input) -- else -- if(input == "a" or input == "A") then -- return 10 -- elseif(input == "b" or input == "B") then -- return 11 -- elseif(input == "c" or input == "C") then -- return 12 -- elseif(input == "d" or input == "D") then -- return 13 -- elseif(input == "e" or input == "E") then -- return 14 -- elseif(input == "f" or input == "F") then -- return 15 -- end -- end -- return -1 return tonumber(input, 16) end