Skip to content

Instantly share code, notes, and snippets.

@sakamoto-poteko
Created July 15, 2016 05:30
Show Gist options
  • Save sakamoto-poteko/cc0964b52e9b8a28beedb268701f0402 to your computer and use it in GitHub Desktop.
Save sakamoto-poteko/cc0964b52e9b8a28beedb268701f0402 to your computer and use it in GitHub Desktop.
ICAO 9303 Checksum
int checksum(const char *str)
{
int weight[] = {7, 3, 1};
int checksum = 0;
int len = strlen(str);
for (int i = 0; i < len; ++i) {
int mappedNum = 0;
if (isdigit(str[i])) {
mappedNum = str[i] - '0';
} else if (isupper(str[i])) {
mappedNum = str[i] - 'A' + 10;
} else if (str[i] == '<') {
mappedNum = 0;
}
checksum += weight[i % 3] * mappedNum;
}
checksum = checksum % 10;
return checksum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment