Shop OBEX P1 Docs P2 Docs Learn Events
Does anyone have a snippet for a "cyclic redundancy check"? — Parallax Forums

Does anyone have a snippet for a "cyclic redundancy check"?

idbruceidbruce Posts: 6,197
edited 2011-01-20 13:38 in Propeller 1
lllllllllllllllllllllllll

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-19 20:30
    PUB ComputeCRC(ptr, num)
      repeat num
        result ^= (byte[ptr++] << 8)
        repeat 8
          result <<= 1
          if result & $10000
            result ^= $1021
      result &= $ffff
    
  • idbruceidbruce Posts: 6,197
    edited 2011-01-19 20:33
    Thanks Dave

    That was quick. Have you used this successfully?

    I assume the ptr is to the data. And what is an example of a good "num" parameter?

    Bruce
  • idbruceidbruce Posts: 6,197
    edited 2011-01-19 20:35
    Dave

    If at all possible, could you please provide an example of use on both ends? Never mind, I think I got it.

    Bruce
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-19 21:38
    Bruce,
    If you look at the code, you'll see that the CRC calculation loop is repeated "num" times, so "num" must be the byte count.
  • idbruceidbruce Posts: 6,197
    edited 2011-01-19 22:13
    Mike

    Thanks for THAT information, I thought it was an arbitrary number, that would produce different hash results. WOW that would have had me going in a mental loop for a while.

    Bruce
  • idbruceidbruce Posts: 6,197
    edited 2011-01-19 22:17
    LOL Did I say that right? It is a hash alogorithim is it not?
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-01-20 10:26
    Bruce,

    The code that I posted will generate the stadard ITU 16-bit CRC for a block of "num" bytes starting at the address in "ptr". This routine assumes that the data is transmitted LSB first. If the data is transmitted MSB first a different routine would be needed, which would use a polynomial word of $8408 instead of $1021, which is the bit-reversed version of $8408. If you want to use this to generate a 16-bit hash code of a string you would do something like hash := ComputeCRC(@strbuf, strsize(@strbuf))

    Dave
  • idbruceidbruce Posts: 6,197
    edited 2011-01-20 13:38
    Dave

    Thanks for that information. I appreciate you sharing your snippet :)

    Bruce
Sign In or Register to comment.