Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CRC Implementation Help

Status
Not open for further replies.

beefeater267

Programmer
Apr 6, 2005
79
Hi,

I'm new to C++, and I need to implement a CRC algorithm as a function, and i'm having trouble doing so.

The polynomial specified is : x16 + x15 + x10 + x3

Can anyone help me out with this one? I've been stuck on it for a while.

Thanks
 
Like this?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Salem,

No, not like that. This is not a standard CRC algorithm.
 
This is not a standard CRC algorithm...
There are several standards for CRCs. You want so-called XMODEM/CRC.
See, for example: From the link above:
Code:
/*
* This function calculates the CRC used by the XMODEM/CRC Protocol
* The first argument is a pointer to the message block.
* The second argument is the number of bytes in the message block.
* The function returns an integer which contains the CRC.
* The low order 16 bits are the coefficients of the CRC.
*/
int calcrc(ptr, count)
char *ptr;
int count;
More.. {
int crc, i;

crc = 0;
while (--count >= 0) {
crc = crc ^ (int)*ptr++ << 8;
for (i = 0; i < 8; ++i)
if (crc & 0x8000)
crc = crc << 1 ^ 0x1021;
else
crc = crc << 1;
}
return (crc & 0xFFFF);
}
 
ArkM -

Thanks for the help. I'll try it out and let you know how it goes!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top