Advertisement
Tatantyler

Cyclic Redundancy Checks - Lua

Jan 23rd, 2013
555
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.63 KB | None | 0 0
  1. -- **********************************************************************************************
  2. --  CRC_Calculate - Calculate a 32-bit CRC based on a given polynomial and block of data.
  3. --  This function caluclates a CRC checksum.
  4. --  Arguments:
  5. --  gen_poly - The generator polynomial. (MSB-0)
  6. --  data - A table with the data to run the CRC on.
  7. --  Returns:
  8. --  The checksum
  9.  
  10. function calculate_CRC(gen_poly, data)
  11.     local remainder = 0
  12.     for i=1, #data do
  13.         remainder = bit.bxor(remainder, bit.blshift(data[i], 24))
  14.         for i=1, 8 do
  15.             if (bit.band(remainder, 0x80000000) > 0) then
  16.                 remainder = bit.bxor(bit.blshift(remainder,1), gen_poly)
  17.             else
  18.                 remainder = bit.blshift(remainder,1)
  19.             end
  20.         end
  21.     end
  22.     return remainder
  23. end
  24.  
  25. -- **********************************************************************************************
  26. -- intToHex() - Convert a number to a hexadecimal string
  27. -- Arguments:
  28. --  input - The input number
  29. -- Returns:
  30. --  string output
  31. -- This function is deprecated, use string.format("%X", input) instead. (Thanks to tomass1996 for pointing this out)
  32.  
  33. function intToHex(input)
  34.     -- local output = {}
  35.     -- local iter = 1
  36.     -- while true do
  37.         -- output[iter] = input % 16
  38.         -- input = math.floor((input - output[iter]) / 16)
  39.         -- if(output[iter] == 10) then
  40.             -- output[iter] = "A"
  41.         -- elseif(output[iter] == 11) then
  42.             -- output[iter] = "B"
  43.         -- elseif(output[iter] == 12) then
  44.             -- output[iter] = "C"
  45.         -- elseif(output[iter] == 13) then
  46.             -- output[iter] = "D"
  47.         -- elseif(output[iter] == 14) then
  48.             -- output[iter] = "E"
  49.         -- elseif(output[iter] == 15) then
  50.             -- output[iter] = "F"
  51.         -- end
  52.         -- if ( input == 0 ) then
  53.             -- return table.concat(output)
  54.         -- end
  55.         -- iter = iter+1
  56.     -- end
  57.     return string.format("%X", input)
  58. end
  59.  
  60. -- **********************************************************************************************
  61. -- intToHex() - Convert a hexadecimal digit into a number TYPE ("string" to "number")
  62. -- Arguments:
  63. --  input - The input hex digit
  64. -- Returns:
  65. --  number output
  66. -- This function is deprecated, use tonumber(input, 16) instead
  67.  
  68. function hexToInt(input)
  69.     -- if (tonumber(input)) then
  70.         -- return tonumber(input)
  71.     -- else
  72.         -- if(input == "a" or input == "A") then
  73.             -- return 10
  74.         -- elseif(input == "b" or input == "B") then
  75.             -- return 11
  76.         -- elseif(input == "c" or input == "C") then
  77.             -- return 12
  78.         -- elseif(input == "d" or input == "D") then
  79.             -- return 13
  80.         -- elseif(input == "e" or input == "E") then
  81.             -- return 14
  82.         -- elseif(input == "f" or input == "F") then
  83.             -- return 15
  84.         -- end
  85.     -- end
  86.     -- return -1
  87.     return tonumber(input, 16)
  88. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement