write a c# code to compute 15 digits imei from first 14 digits
Sure, here's a C# code to compute the 15th digit of an IMEI number from the first 14 digits:
using System;
class Program
{
static void Main(string[] args)
{
string imeiDigits = "12345678901234"; // Replace with your first 14 digits
if (imeiDigits.Length != 14)
{
Console.WriteLine("Invalid IMEI length. IMEI should have 14 digits.");
return;
}
int sum = 0;
bool isEven = true;
for (int i = imeiDigits.Length - 1; i >= 0; i--)
{
int digit = int.Parse(imeiDigits[i].ToString());
if (isEven)
sum += digit;
else
sum += (digit * 2) % 10 + (digit * 2) / 10;
isEven = !isEven;
}
int lastDigit = (10 - (sum % 10)) % 10;
Console.WriteLine("Computed IMEI: " + imeiDigits + lastDigit);
}
}
In this code, you need to replace the imeiDigits variable with the first 14 digits of your IMEI number. The program then computes the 15th digit according to the Luhn algorithm used for IMEI validation. The computed IMEI number is then printed to the console
原文地址: https://www.cveoy.top/t/topic/h47N 著作权归作者所有。请勿转载和采集!