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

# One’s Complement Fletcher-16 Checksum Calculation

def fletcher_checksum(init_value: int, bytes: list):

    # Initialize One’s Complement Fletcher Checksum

    checksum = init_value

    # Loop through all data bytes, each stored at oneByte

    for one_byte in bytes:

        # Update lower checksum byte

        temp_low = checksum & 0xff

        temp_low += one_byte

        if (temp_low & 0x100) != 0:

            temp_low += 1

 

        # Update higher checksum byte

        temp_high = checksum >> 8

        temp_high += temp_low & 0xff

        if (temp_high & 0x100) != 0:

            temp_high += 1

 

        checksum = ((temp_low & 0xff) | (temp_high & 0xff) << 8)

    return checksum