
October 1st, 2002, 01:39 AM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
How about some code like this:
Code:
unsigned char compute_checksum(unsigned char *buf) {
unsigned char *ch, result = 0;
/* buf is assumed to be null terminated */
for (ch = buf; *ch; ch++)
result += (*ch);
return result;
}
....
unsigned char checksum;
....
....
checksum = compute_checksum(buffer);
/* add checksum to end of buffer */
buffer[len] = checksum;
buffer[len+1] = '\0'; /* buffer assumed to be at least len+1 chars */
Please note that I typed this code off the top of my head, so it may have a few syntax errors. This isn't even a very good checksum algorithm, it just adds the ascii value of the chars up. You'd probably want to do a standard 16 or 32 bit CRC algorithm to ensure more reliability. You can probably google for those  Hope this helps!
|