Skip to content

Instantly share code, notes, and snippets.

@huwcbjones
Created December 18, 2018 19:01
Show Gist options
  • Save huwcbjones/6117f5adc365cacbeded6c3f6b103131 to your computer and use it in GitHub Desktop.
Save huwcbjones/6117f5adc365cacbeded6c3f6b103131 to your computer and use it in GitHub Desktop.
Hy-Tek Checksum Functions
def calculate_hy3_sum(line: str) -> int:
data = "{:<128}".format(line) # pad to 128 chars
odd_sum = sum([ord(data[c]) for c in range(0, len(data), 2)]) # sum odd chars
even_sum = sum([ord(data[c]) for c in range(1, len(data), 2)]) # sum even chars
checksum = math.trunc((odd_sum + 2 * even_sum) / 21) # add odd + 2 * even, then /21, ignore decimal
checksum += 205 # add 205 to result
checksum = int(str(checksum)[:-3:-1]) # take last 2 digits of result and reverse
return checksum
def calculate_cl2_sum(cls, line: str) -> str:
data = "{:<156}".format(line) # pad to 156 chars
char_sum = sum([ord(data[c]) for c in range(0, len(data))]) # sum chars
# even_sum = sum([ord(data[c]) for c in range(1, len(data), 2)]) # sum even chars
checksum = math.trunc(char_sum/19) # divide char sum by 21, ignore decimal
checksum += 211 # add 211 to result
checksum = str(checksum)[:-3:-1] # take last 2 digits of result and reverse
prefix = " "
# if data[:2] == "D0":
# prefix = "N"
return "{}N{}".format(prefix, checksum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment