Fletcher checksum???
Author Message
Fletcher checksum???

Hello, I'm short of the algorithm/code for the fletcher checksum.
Can you be of assistance?

Yh.

Sun, 21 Apr 2002 03:00:00 GMT
Fletcher checksum???
int calculateFletcherChecksum(BYTE *BytesToChecksum, int
NumberOfBytesToChecksum)
{

int i, iNumberOfBytesToChecksum = NumberOfBytesToChecksum;

int iSum1 = 0;
int iSum2 = 0;

int iCheck1 = 0;
int iCheck2 = 0;

int iChecksum;

BYTE *pBYTE = BytesToChecksum;

static char *pcTitle = "calculateFletcherChecksum()";

for (i = 0; i < iNumberOfBytesToChecksum; i++) {
iSum1 = (iSum1 + *pBYTE) % 255;
iSum2 = (iSum1 + iSum2) % 255;
pBYTE++;
}

iCheck1 = 255 - ((iSum1 + iSum2) % 255);

iCheck2 = 255 - ((iSum1 + iCheck1) % 255);

iChecksum = (int) (iCheck1 & 0x00ff);
iChecksum |= (int) ((iCheck2 & 0x00ff) << 8);

return (iChecksum);

Quote:
}

Sincerely,

MarcW. (No warranty given or implied...)

Quote:

> Hello, I'm short of the algorithm/code for the fletcher checksum.
> Can you be of assistance?

> Thanks in advance
> Yh.

Sun, 21 Apr 2002 03:00:00 GMT
Fletcher checksum???
Quote:
> Hello, I'm short of the algorithm/code for the fletcher
checksum.
> Can you be of assistance?

Here is the short description of this algorithm from the
RFC1146:
APPENDIX I:  The 8-bit Fletcher Checksum Algorithm
The 8-bit Fletcher Checksum Algorithm is calculated over a
sequence
of data octets (call them D[1] through D[N]) by maintaining
2
unsigned 1's-complement 8-bit accumulators A and B whose
contents are
initially zero, and performing the following loop where i
ranges from
1 to N:
A := A + D[i]
B := B + A
It can be shown that at the end of the loop A will contain
the 8-bit
1's complement sum of all octets in the datagram, and that B
will
contain (N)D[1] + (N-1)D[2] + ... + D[N].
[...]
APPENDIX II:  The 16-bit Fletcher Checksum Algorithm
The 16-bit Fletcher Checksum algorithm proceeds in precisely
the same
manner as the 8-bit checksum algorithm,, except that A, B
and the
D[i] are 16-bit quantities.
So the test code in C for 8-bit Fletcher checksum will look
like this:
#include <stdio.h>
int main (void)
{
unsigned char d[] = "Test string";
unsigned char a = 0, b = 0;
int n = strlen(d);
int i;
for (i = 0; i < n; i++) {
a += d[i];
b += a;
}
printf("The checksum is (0x%02X, 0x%02X)\n",
(unsigned int) a, (unsigned int) b);
return 0;
Quote:
}

The actual checksum is composed from A and B. For example,
the first byte of 8-bit checksum is A and second is B.
Note that 8-bit checksum is 16 bits long and 16-bit checksum
is 32 bits long.
Quote:
> Thanks in advance
> Yh.

WBW,
Const

--
This answer is courtesy of QuestionExchange.com
http://www.questionexchange.com/showUsenetGuest.jhtml?ans_id=7277&cus...

Fri, 26 Apr 2002 03:00:00 GMT

 Page 1 of 1 [ 3 post ]

Relevant Pages