#include <stdio.h>
//351302984184056
#define IMEI "35130298418405"
int getIMEICheckDigit(char *imei)
{
int i, sum1 = 0, sum2 = 0, temp, result = 0;
for (i = 0; i < 14; i++) {
if (i % 2 == 0) {
//奇数位和
sum1 += imei[i] - '0';
} else {
//偶数位乘2 个位十位和
temp = (imei[i] - '0') * 2;
sum2 += temp;
if (temp >= 10) {
sum2 -= 9;
}
}
}
temp = (sum1 + sum2) % 10;
if (temp != 0) {
result = 10 - temp;
}
return result;
}
int main(int argc, char *agrv[])
{
printf("IMEI %s check digit is %d\n", IMEI, getIMEICheckDigit(IMEI));
return 0;
}
评论已关闭