Created
July 15, 2016 05:30
-
-
Save sakamoto-poteko/cc0964b52e9b8a28beedb268701f0402 to your computer and use it in GitHub Desktop.
ICAO 9303 Checksum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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