PreviousNext
C#
Help > Appendix > One’s Complement Fletcher-16 Checksum Calculation > C#

public static UInt16 FletcherChecksum ( byte[] bytes )

{

  // Initialize One’s Complement Fletcher Checksum

  UInt16 checkSum = “initial value”;

 

  // Loop through all data bytes, each stored at oneByte

  foreach ( byte oneByte in bytes )

  {

    // Update lower checksum byte

    int tempL = checkSum & 0xff;

    tempL += oneByte;

    if ( ( tempL & 0x100 ) != 0 )

      tempL++;

 

    // Update higher checksum byte

    int tempH = checkSum >> 8;

    tempH += tempL & 0xff;

    if ( ( tempH & 0x100 ) != 0 )

      tempH++;

 

    checkSum = (UInt16)( ( tempL & 0xff ) | ( tempH & 0xff ) << 8 );

  }

 

  return checkSum;

}