C代码
int calc_imei(char inp_imei[16], char out_imei[12])
{
char out_mask[12] = {0xAB, 0xA0, 0x6F, 0x2F, 0x1F, 0x1E, 0x9A, 0x45, 0x0, 0x0, 0x0, 0x0};
int i=0, j=0;
for (i=0, j=0; i < 15; i++, j++)
{
if (inp_imei[i] < '0' || inp_imei[i] > '9')
{
return 1;
}
out_imei[j] = (inp_imei[i] - '0');
if (i >= 14)
break;
if (inp_imei[i+1] < '0' || inp_imei[i+1] > '9')
{
return 1;
}
out_imei[j] += ((inp_imei[i+1] - '0') << 4);
out_imei[j] = out_imei[j] ^ out_mask[j];
i++;
}
out_imei[j] = out_imei[j] ^ out_mask[j];
out_imei[8] = 0x57;
out_imei[9] = 0xDB;
out_imei[10] = out_imei[11] = 0;
for (i = 0; i < 10; i++)
{
if (i & 0x1)
{
out_imei[11] += out_imei[i];
}
else
{
out_imei[10] += out_imei[i];
}
}
return 0;
}
自己写的转JAVA
char[] out_mask = { 0xAB, 0xA0, 0x6F, 0x2F, 0x1F, 0x1E, 0x9A, 0x45,
0x0, 0x0, 0x0, 0x0 };
char[] out_imei = new char[12];
int i = 0, j = 0;
for (i = 0, j = 0; i < 15; i++, j++) {
if (inp_imei[i] < '0' || inp_imei[i] > '9') {
return null;
}
out_imei[j] = (char) (inp_imei[i] - '0');
if (i >= 14)
break;
if (inp_imei[i + 1] < '0' || inp_imei[i + 1] > '9') {
return null;
}
out_imei[j] += ((inp_imei[i + 1] - '0') << 4);
out_imei[j] = (char) (out_imei[j] ^ out_mask[j]);
i++;
}
out_imei[j] = (char) (out_imei[j] ^ out_mask[j]);
out_imei[8] = 0x57;
out_imei[9] = 0xDB;
out_imei[10] = out_imei[11] = 0;
for (i = 0; i < 10; i++) {
if ((i & 0x1) != 0) {
out_imei[11] += out_imei[i];
} else {
out_imei[10] += out_imei[i];
}
}
return out_imei;
}
为什么不对 55555555555555555555