Mohsen Deeb wrote:
> Hi;
> 
> I found this code on some websites, but I don't know how to check if it is
> right or wrong.
> 
> Given that;
> 
> UINT16 = unsigned int
> UINT8 = unsigned char
> The require CRC is the CRC16-CCITT
> 
>  UINT16 crcByte(UINT16 crc, UINT8 b)
>  {
>      UINT8 t = 8;
> 
>     crc = crc ^ b << 8;
> 
>       do
>       {
>           if (crc & 0x8000)
>                   crc = crc << 1 ^ 0x1021;
> 
>           else
>                   crc = crc << 1;
> 
>     }while (--t);
> 
> 
>       return crc;
>  }
> 
> 
> Thanks and Best Regards
> 
> Mohsen

There are some freeware applications out there that allow you to 
generate a bunch of different CRCs for a given data set.  Just find one 
that does 16-bit CRCs.  You've run into one of my pet peeves:

http://cubicspot.blogspot.com/2007/09/standards-documentation-is-annoying.html

To answer your question, it looks correct.  There are no reflections and 
the poly is 0x1021, which is the correct poly.  The only issue you 
probably have is "What do I do with this function?"  The only thing you 
are likely missing is the initial value to pass in as 'crc'.  0xFFFF is 
what I've got as the correct initial value for all standardized CRC-16s. 
  To answer your next question, "How do I test this?"  Enter a string 
into both the freeware tool you find and your app.  If the value matches 
up (typically in hex), then your implementation is probably correct. 
Maybe.  Your tool AND their tool could both be wrong and yet produce the 
same answer (i.e. both using the same flawed source code).

Here's a sample from my personal, automated test suite:

Using no reflections, a 0x1021 poly, and an initial 0xFFFF, the string:
"message digest" (without the quotes) should be 0x32CC.

-- 
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197

*NEW* MyTaskFocus 1.1
Get on task.  Stay on task.

http://www.CubicleSoft.com/MyTaskFocus/

Reply via email to