View previous topic :: View next topic |
Author |
Message |
pr83
Joined: 20 Jul 2006 Posts: 15
|
checksum |
Posted: Tue Jan 02, 2007 7:08 pm |
|
|
I have an array of 12 data bytes. I wrote code for checksum, where the check sum is the one's compliment of the sum of the 12 data bytes. I have pasted the code below. Do you see any mistakes, cause I dont seem to be getting the correct checksum.
Code: |
BYTE temp;
// ShortPack[13] is of type BYTE as well.
for( i=0;i<11;i++)
{
temp = ShortPack[i]+ShortPack[i+1];
i++;
}
ShortPack[12]= temp^0xFF ; // checksum value
|
!!!!! |
|
|
Ttlmah Guest
|
|
Posted: Wed Jan 03, 2007 3:51 am |
|
|
Think about what you are doing.
First time through the loop. temp, becomes the sum of entry 0, and entry 1. Second time through the loop, this value is thrown away, and temp becomes the sum of entry 2, and entry 3....
The last time through the loop, temp becomes the sum of entry 10 and entry 11. Not the sum of the whole lot...
To add up the values, you need:
Code: |
//Initialise 'temp', so it is zero
BYTE temp=0;
// ShortPack[13] is of type BYTE as well.
for( i=0;i<12;i++) {
temp += ShortPack[i];
}
ShortPack[12]= temp^0xFF ; // checksum value
|
Best Wishes |
|
|
pr83
Joined: 20 Jul 2006 Posts: 15
|
|
Posted: Wed Jan 03, 2007 7:41 am |
|
|
Thanks Ttlmah.
You are correct. |
|
|
|