This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Lua CRC16-ccitt example
#1
Hi, just sharing
Code:
data = ('njhyTFGV')
init = 0xffff
crc = init
function crc16_ccitt(crc, data)
  msb = bit.rshift(crc , 8)
  lsb = bit.band(crc , 255)
  --log(msb, lsb)
  for i = 1, #data do
    strb = string.byte(data, i, i)
    x = bit.bxor(strb, msb)
    --log(strb,msb, x)
    x = bit.bxor(x,  bit.rshift(x,4))
    --log(x)
    msb = bit.band(bit.bxor(lsb , bit.rshift(x , 3) , bit.lshift(x , 4)) , 255)
    lsb = bit.band(bit.bxor(x , bit.lshift(x , 5)) , 255)
    log(msb, lsb, x)
    end
  res = lsb + bit.lshift(msb, 8)
  return res , string.format('%x', res)
end
 
log(crc16_ccitt(crc, data))
results was tested with this service
https://crccalc.com/

original code was made on Python, was taken here in comments
https://stackoverflow.com/questions/2523...alculation
Reply


Forum Jump: