Click to See Complete Forum and Search --> : string checksum


Moore
June 20th, 2007, 01:06 PM
hi,

I'm looking for information/algorithm/possible solutions to calculate the checksum of a string that may be 7 or 8 bit encoded.

is there a generic CRC algorithm that can be used.

I would appreciate any advice.

thanks.

TheCPUWizard
June 20th, 2007, 01:09 PM
What do you want a checksum or a CRC (CRC-16 mapped to Radix64??)

S_M_A
June 20th, 2007, 01:13 PM
It's not that hard to write one. Here's a couple of links that explains the details.

http://www.employees.org/~surendra/asic/crc.html
http://www.ece.msstate.edu/~reese/EE4743/crcexplained.htm
http://www.repairfaq.org/filipg/LINK/F_crc_v3.html
http://www.ross.net/crc/links.html

Moore
June 20th, 2007, 06:03 PM
hello,

I would like to consider both a checksum and a CRC to see what is involved.

I presume the CRC would be more complex but more reliable.

Is there a proven and defined method/implementations for performing a simple checksum on strings which can be 7/8 bit encoded?


I'm not sure what is involved with radix64 but from your reply I guess it is directly related to CRC and the encoding of chars. I can research for more info.

If you can provide more info or sample, that would be great.

thank you.

S_M_A
June 20th, 2007, 06:50 PM
A plain checksum is as you say not reliable. I think this is described well in section 2 of http://www.employees.org/~surendra/asic/crc.html. I guess that the most common plain checksum is where you sum all bytes modulo 8/16 and then add either the sum or the negated sum to the stream.

The documents also describes how to make a n-bit CRC sum (at least one of them) and this can be applied to any data, 7/8 bits doesn't matter. Either you decide to pack your 7 bit data in a new set of 8 bit data or you pack them as they are.

The hard part about CRC is to decide init value, ideally this shall be based on a good knowledge of the actual data to be handled (also described in docs).

TheCPUWizard
June 20th, 2007, 10:03 PM
I guess that the most common plain checksum is where you sum all bytes modulo 8/16 and then add either the sum or the negated sum to the stream.
Well if you did THAT then you would only be checking the low order 3 or 4 bits (respectively). :eek: :eek:

S_M_A
June 21st, 2007, 02:15 AM
Typo... should of course be modulo 8/16 bits

SuperKoko
June 21st, 2007, 04:18 AM
What do you want a checksum or a CRC (CRC-16 mapped to Radix64??)

The term "checksum", initially meant "sum of bytes/words modulo a power of two", but is now more or less abused to mean "a hash algorithm" like CRC32, MD5 and SHA1.

SuperKoko
June 21st, 2007, 04:19 AM
Typo... should of course be modulo 8/16 bits
More rigourously, you should have said modulo 2**8 or 2**16.

S_M_A
June 21st, 2007, 04:56 AM
Maybe so but in this context (without typo) I don't think that my post will be misunderstood, at least not if the link is read as well.

Moore
June 21st, 2007, 06:53 AM
hello, thanks for replies

S_M_A
for simple checksum, are you talking about something like the following



int stringChecksum(char *string)
{
char *pString;
int cksum;

pString = string;
cksum = 0;

while (*pString != '\0')
cksum += (int)(*pString++) - '0';

cksum = cksum % 8; // or 16?? can you modulo other value?
// what is the impact/deciding factor on what value to use for modulo?
return(cksum); // should we do any else on cksum value before returning?
}


I'm not sure yet how this may be impacted by 7/8 bit encoded chars.
Would you need to pad each 7 bit char with an extra bit?
Can you let me know if this on on the right track and/or correct?

S_M_A
June 21st, 2007, 07:25 AM
Yes that's what I think of for a simple checksum, 7 or 8 bits doesnt matter.

I would however declare checksum and return type as char instead.
char stringChecksum(char* string)
{
char cksum = 0;
while( *string ) cksum += *string++;
return cksum; // or maybe return -cksum
}

Moore
June 21st, 2007, 07:34 AM
hello,
and what about the modulo opertion?

thanks

Moore
June 22nd, 2007, 09:04 AM
hello,

can you tell me the reason for having the data type of "char" for cksum and return type please?

should this be converted to int?

thanks.

S_M_A
June 22nd, 2007, 09:29 AM
The module operation is kind of solved automatically since cksum overflows. After every addition cksum holds (cksum + value) % 256

I choosed char type since then you can use the function like (pseudo code)
'anytype' data;
char sendBuf[sizeof(data)+1];
// Copy data to send buffer
memcpy(sendBuf,&data,sizeof(data));
// Add 8 bit checksum
sendBuf[sizeof(data)] = CalcChecksum(sendBuf,sizeof(data));
Where I've changed the checksum function to accept void* and length instead of just doing calculation for a null-terminated string