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

Sum bytes in a string?

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



Joined: 14 Feb 2008
Posts: 18
Location: Cumbria

View user's profile Send private message

Sum bytes in a string?
PostPosted: Mon Jul 14, 2008 1:56 am     Reply with quote

Maybe just a Monday morning thing Smile but how do I sum all the bytes in a string (mod 256) to enable a check sum to be developed?

Kevin - M0KHZ
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jul 14, 2008 3:34 am     Reply with quote

Code:
char MyString[] = {"Hello world."};
char *ptr;
int8 Checksum;

Checksum = 0;

ptr = MyString;
while (ptr != 0)
{
  Checksum += *ptr;
}


Instead of a modulo 256 checksum a CRC8 would give a better error detection. For an efficient implementation see
http://www.ccsinfo.com/forum/viewtopic.php?t=26264
Guest








PostPosted: Mon Jul 14, 2008 4:52 am     Reply with quote

Thanks ckielstra, I knew the solution would be straight forward. The reason for mod256 is due to the ‘other end’ of the communication protocol requiring it, the message structure is fixed with a header and footer, I just need to sum the elements of the string.

Kevin – M0KHZ
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