The result of 0x29b1
is for the "false" CCITT CRC-16 (link to CRC catalog). Which is apparently the one you need. From the catalog:
width=16 poly=0x1021 init=0xffff refin=false refout=false xorout=0x0000 check=0x29b1 name="CRC-16/CCITT-FALSE"
So there is no bit reversal (refin
, refout
false). The CRC is initialized with 0xffff
and is not post-processed.
To fix your code with the least changes:
if (wLength == 0)
return wCrc;
do
{
for (i=0, wData=((unsigned int)0xff & *pData++) << 8; i < 8; i++, wData <<= 1)
{
if ((wCrc & 0x8000) ^ (wData & 0x8000))
wCrc = (wCrc << 1) ^ 0x1021;
else wCrc <<= 1;
}
} while (--wLength);
return wCrc & 0xffff;
or to do it more reasonably:
while (wLength--) {
wCrc ^= *(unsigned char *)pData++ << 8;
for (i=0; i < 8; i++)
wCrc = wCrc & 0x8000 ? (wCrc << 1) ^ 0x1021 : wCrc << 1;
}
return wCrc & 0xffff;