CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

checksum

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pr83



Joined: 20 Jul 2006
Posts: 15

View user's profile Send private message

checksum
PostPosted: Tue Jan 02, 2007 7:08 pm     Reply with quote

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







PostPosted: Wed Jan 03, 2007 3:51 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 03, 2007 7:41 am     Reply with quote

Thanks Ttlmah.
You are correct.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group