Fletchers Checksum
-
- Posts: 130
- Joined: Sat Feb 26, 2011 4:26 am
- Location: South Africa
- Has thanked: 25 times
- Been thanked: 11 times
- Contact:
Fletchers Checksum
Hi guys
How can you do a 2 byte Fletchers Checksum over a range of say 4 bytes in Flowcode V5
How can you do a 2 byte Fletchers Checksum over a range of say 4 bytes in Flowcode V5
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact:
Re: Fletchers Checksum
Hi,
Will that serve your purpose...If so we can make it in flowcode easily...
Will that serve your purpose...If so we can make it in flowcode easily...
The below is a treatment on how to calculate the checksum including the check bytes; i.e., the final result should equal 0, given properly-calculated check bytes. The code by itself, however, will not calculate the check bytes.
An inefficient but straightforward implementation of a C language function to compute the Fletcher-16 checksum of an array of 8-bit data elements follows:On lines 3 and 4, the sums are 16-bit variables so that the additions on lines 9 and 10 will not overflow. The modulo operation is applied to the first sum on line 9 and to the second sum on line 10. Here, this is done after each addition, so that at the end of the while loop the sums are always reduced to 8-bits. At the end of the input data, the two sums are combined into the 16-bit Fletcher checksum value and returned by the function on line 13.Code: Select all
uint16_t Fletcher16( uint8_t* data, int count ) { uint16_t sum1 = 0; uint16_t sum2 = 0; int index; for( index = 0; index < count; ++index ) { sum1 = (sum1 + data[index]) % 255; sum2 = (sum2 + sum1) % 255; } return (sum2 << 8) | sum1; }
Each sum is computed modulo 255 and thus remains less than 0xFF at all times. This implementation will thus never produce the checksum results 0x00FF, 0xFF00 or 0xFFFF. It can produce the checksum result 0x0000, which may not be desirable in some circumstances (e.g. when this value has been reserved to mean "no checksum has been computed").
-
- Posts: 130
- Joined: Sat Feb 26, 2011 4:26 am
- Location: South Africa
- Has thanked: 25 times
- Been thanked: 11 times
- Contact:
Re: Fletchers Checksum
Hi Enamul
I would appreciate a flowcode version. I saw the c code in Wikipedia but cannot make out heads or tales about c code
For now I can only program in flowcode & assembler "Only starting to learn C"
I would appreciate a flowcode version. I saw the c code in Wikipedia but cannot make out heads or tales about c code
For now I can only program in flowcode & assembler "Only starting to learn C"
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact:
Re: Fletchers Checksum
I will do that for you but I guess you missed my question..Will it serve your purpose..
- Enamul
- Posts: 1772
- Joined: Mon Mar 05, 2012 11:34 pm
- Location: Nottingham, UK
- Has thanked: 271 times
- Been thanked: 814 times
- Contact:
Re: Fletchers Checksum
OK..good. I will post to-night after having some play and reading some text about the algorithm.
-
- Posts: 130
- Joined: Sat Feb 26, 2011 4:26 am
- Location: South Africa
- Has thanked: 25 times
- Been thanked: 11 times
- Contact:
Re: Fletchers Checksum
Thanks for the help. In the meantime I will tri and get some results on my side.
- JonnyW
- Posts: 1230
- Joined: Fri Oct 29, 2010 9:13 am
- Location: Matrix Multimedia Ltd
- Has thanked: 63 times
- Been thanked: 290 times
- Contact:
Re: Fletchers Checksum
Evening. Is what you require only ever going to be from a 4 byte value?
You can do this in a single calculation icon if you wish, below is the code to calculate this depending on whether your 4 bytes are a 32-bit value or an array.
You can cut either code block into a single calculation icon which will output the result to 'csum16'.
This should get the result that you want.
You can do this in a single calculation icon if you wish, below is the code to calculate this depending on whether your 4 bytes are a 32-bit value or an array.
You can cut either code block into a single calculation icon which will output the result to 'csum16'.
Code: Select all
// byte1 and byte2 are local byte variables. w is an (un)signed long (32-bit) value you wish to checksum. csum16 is an unsigned short
.byte1 = (w >> 24) + (w >> 16) + (w >> 8) + w
.byte2 = (w >> 24) + ((w >> 16) << 1) + (w >> 8) * 3 + (w << 2)
csum16 = (.byte1 & 0xFF) | (.byte2 << 8)
Code: Select all
// byte1 and byte2 are local byte variables. b is a 4-byte array you wish to checksum. csum16 is an unsigned short
.byte1 = b[3] + b[2] + b[1] + b[0]
.byte2 = b[3] + (b[2] << 1) + b[1] * 3 + (b[0] << 2)
csum16 = (.byte1 & 0xFF) | (.byte2 << 8)
-
- Posts: 130
- Joined: Sat Feb 26, 2011 4:26 am
- Location: South Africa
- Has thanked: 25 times
- Been thanked: 11 times
- Contact:
Re: Fletchers Checksum
Hi JonnyW
Your second code block look like it might work, I'll play around with it to see if I can get it to work.
No the bytes might be an array of up to 8 or more 8bit bytes.Is what you require only ever going to be from a 4 byte value?
Your second code block look like it might work, I'll play around with it to see if I can get it to work.