mason@ryesone.UUCP (Dave Mason) (04/03/86)
I thought I had a working CRC routine, but it doesn't seem to net to
a zero. For background a CRC-CCITT or CRC-16 calculated on a message
(plus 2 zero bytes) gives 2 CRC characters. If you calculate the CRC
on the message with the CRC characters replacing the zero bytes should
give zeros for the CRC. My CRC routine gives a number that is zero mod 256,
but not necessarily 0. Any help would be appreciated. The code follows:
int crc;
main() {
check("asdfks");
check("abc");
check("1231455");
}
check(omsg) char *omsg; {
int i,j;
int ch;
char *msg;
printf(">>>>>>>>\n");
msg=omsg;
crc=0;
while (ch = *msg++)
docrc(ch);
docrc(0);
docrc(0);
i=crc/256 % 256;
j=crc %256;
printf("------\n");
msg=omsg;
crc=0;
while (ch = *msg++)
docrc(ch);
docrc(i);
docrc(j);
printf("crc=%d\n",crc);
}
docrc(ch) int ch; {
register int n;
register int i,t;
n=ch;
n &= 255;
printf("ch=%d ",n);
i=8;
t=crc;
while (--i>=0) {
n<<=1;
t<<=1;
if (n & 256)
++t;
if (crc<0)
t ^= 0x1021;
crc=t;
}
printf("crc=%d\n",crc);
}
Thanks...
--
usenet: ..!utzoo!utcsri!mason Dave Mason, U. Toronto CSRI
..!utzoo!ryesone!mason Dave Mason, Ryerson Polytechnical Institute
CSNET: mason@Toronto
ARPA: mason%Toronto@CSNet-Relay
BITNET: FCTY7053@RYERSON.BITNETmason@ryesone.UUCP (Dave Mason) (04/06/86)
> I thought I had a working CRC routine, but it doesn't seem to net to > a zero. Turns out it *was* right, but I was mangling the checksum when I put it on the line. The error was in my routine to validate the crc. Basically I divided a negative number by 256 as an attempt to shift right by 8. > check(omsg) char *omsg; { > int i,j; > int ch; > char *msg; > printf(">>>>>>>>\n"); > msg=omsg; > crc=0; > while (ch = *msg++) > docrc(ch); > docrc(0); > docrc(0); > i=crc/256 % 256; <<<<<< i=crc>>8; > j=crc %256; > printf("------\n"); > msg=omsg; > crc=0; > while (ch = *msg++) > docrc(ch); > docrc(i); > docrc(j); > printf("crc=%d\n",crc); > } ------ I wish computers were as predictable as dice! -- usenet: ..!utzoo!utcsri!mason Dave Mason, U. Toronto CSRI ..!utzoo!ryesone!mason Dave Mason, Ryerson Polytechnical Institute CSNET: mason@Toronto ARPA: mason%Toronto@CSNet-Relay BITNET: FCTY7053@RYERSON.BITNET