Check IMEI Number by Luhn algorithm.

check digit (d9) = [7x(d1 + d4 + d7) + 3(d2 + d5+ d8) + 9(d3 + d6)] mod 10

The check digit is validated in three steps:

Starting from the right, double a digit every two digits (e.g., 5 → 10).
Sum the digits (e.g., 10 → 1+0). Check if the sum is divisible by 10.
Conversely, one can calculate the IMEI by choosing the check digit that would give a sum divisible by 10.
For example:

IMEI 49015420323751?

IMEI 4 9 0 1 5 4 2 0 3 2 3 7 5 1 ?
Double every other 4 18 0 2 5 8 2 0 3 4 3 14 5 2 ?
Sum digits 4 + (1 + 8) + 0 + 2 + 5 + 8 + 2 + 0 + 3 + 4 + 3 + (1 + 4) + 5 + 2 + ? = 52 + ?

int ibm_mod10(char a[])
{
int i, len, mul, sum;
int chksum;
int m10;

len = strlen(a); //length of the string passed

chksum = char2int(a[len-1]); //Last element should be the checksum

mul = 2; sum=0;

for(i=len-2; i>=0; i–)
{
if( (char2int(a[i])* mul) >= 10 )
sum += ((char2int(a[i])* mul ) / 10) +((char2int(a[i])* mul ) % 10 );

else
sum += char2int(a[i])* mul;

if(mul==2) mul=1;
else mul=2;
}

m10 = sum%10;

if(m10) m10 = 10 – m10;

if(m10 == chksum)
return 1;

else
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s