need help with this CRC generation snip

I'm a assembly programmer that's been trying to calculate some CRC's for a device I'm trying to make work. The Mfgr of the device published a code snip for the CRC generator that I tried to make work in "codepad"
http://codepad.org/d2u8bHLW
codepad is complaining about line5 (expected unqualified-id before 'for'.
This particular snip is trying to generate a CRC of the 3 bytes 0x02 0x00 0x0D and the correct CRC result should be 0x5D. Are there any kind souls out there who could help me understand how to make this work, and possibly point me in the direction of an on-line compiler that I could use to run this and generate the CRC's I need to make the commands to my instrument work?
Another data set 0x02 0x00 0x81 should result with CRC = 0x39
Thanks very much for any help for this old-school assembly guy..
Doug.

1
2
3
4
5
6
7
8
9
10
int nPacketOffset;
long lCRCValue = 0;
int nSize = 3;
int pPacket[3] = {0x02,0x00,0x0D};
for(nPacketOffset = 0; nPacketOffset < nSize; nPacketOffset++)
{
lCRCValue = (lCRCValue >> 4) ^ (((lCRCValue ^ pPacket[nPacketOffset]) & 0x0F) * 0x1081);
lCRCValue = (lCRCValue >> 4) ^ (((lCRCValue ^ (pPacket[nPacketOffset]>>4)) & 0x0F) * 0x1081);
}
return (char) (lCRCValue & 0x000000FF);
Seems ok.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

unsigned char crc(int pPacket[], int nSize)
{
	int nPacketOffset;
	long lCRCValue = 0;
	for(nPacketOffset = 0; nPacketOffset < nSize; nPacketOffset++)
	{
		lCRCValue = (lCRCValue >> 4) ^ (((lCRCValue ^ pPacket[nPacketOffset]) & 0x0F) * 0x1081);
		lCRCValue = (lCRCValue >> 4) ^ (((lCRCValue ^ (pPacket[nPacketOffset]>>4)) & 0x0F) * 0x1081);
	}

	return lCRCValue & 0x000000FF;
}

int main()
{
	int a[] = {0x02,0x00,0x0D}, b[] = {0x02,0x00,0x81};

	printf("0x%x\n", crc(a, 3));
	printf("0x%x\n", crc(b, 3));
}

0x5d
0x39
Last edited on
This worked perfectly!
Thanks very much!
Doug.
Topic archived. No new replies allowed.