vb.net 是否可以验证 IMEI 号码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25229648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:01:22  来源:igfitidea点击:

Is it possible to validate IMEI Number?

cvb.netimei

提问by Kichu

For a mobile shop application, I need to validate an IMEI number. I know how to validate based on input length, but is their any other mechanism for validating the input number? Is there any built-in function that can achieve this?

对于移动商店应用程序,我需要验证IMEI 号码。我知道如何根据输入长度进行验证,但是它们是否还有其他用于验证输入数字的机制?是否有任何内置功能可以实现这一点?

Logic from any language is accepted, and appreciated.

任何语言的逻辑都被接受和赞赏。

回答by Karl Nicoll

A search suggests that there isn't a built-infunction that will validate an IMEI number, but there is a validation method using the Luhn algorithm.

搜索表明没有内置函数可以验证 IMEI 号码,但有一种使用Luhn 算法的验证方法。

General process:

一般流程:

  1. Input IMEI: 490154203237518
  2. Take off the last digit, and remember it: 49015420323751& 8. This last digit 8 is the validation digit.
  3. Double each second digit in the IMEI: 4 18 0 2 5 8 2 0 3 4 3 14 5 2(excluding the validation digit)
  4. Separate this number into single digits: 4 1 8 0 2 5 8 2 0 3 4 3 1 4 5 2(notice that 18and 14have been split).
  5. Add up all the numbers: 4+1+8+0+2+5+8+2+0+3+4+3+1+4+5+2= 52
  6. Take your resulting number, remember it, and round it up to the nearest multiple of ten: 60.
  7. Subtract your original number from the rounded-up number: 60 - 52= 8.
  8. Compare the result to your original validation digit. If the two numbers match, your IMEI is valid.
  1. 输入IMEI: 490154203237518
  2. 去掉最后一个数字,记住它:49015420323751& 8。最后一位数字 8 是验证数字。
  3. IMEI 中每第二个数字加倍:(4 18 0 2 5 8 2 0 3 4 3 14 5 2不包括验证数字)
  4. 将此数字分隔为单个数字:(4 1 8 0 2 5 8 2 0 3 4 3 1 4 5 2注意1814已被拆分)。
  5. 把所有数字加起来:4+1+8+0+2+5+8+2+0+3+4+3+1+4+5+2=52
  6. 取你得到的数字,记住它,然后把它四舍五入到最接近的十的倍数:60
  7. 从四舍五入的数字中减去您的原始数字:60 - 52= 8
  8. 将结果与您的原始验证数字进行比较。如果这两个数字匹配,则您的 IMEI 有效。

The IMEI given in step 1 above is valid, because the number found in step #7 is 8, which matches the validation digit.

上面第 1 步中给出的 IMEI 是有效的,因为在第 7 步中找到的数字是 8,它与验证数字匹配。

回答by Osvel Alvarez Jacomino

According to the previous answerfrom Karl Nicolli'm created this method in Java.

根据Karl Nicoll之前的回答,我在 Java 中创建了这个方法。

public static int validateImei(String imei) {

    //si la longitud del imei es distinta de 15 es invalido
    if (imei.length() != 15)
        return CheckImei.SHORT_IMEI;

    //si el imei contiene letras es invalido
    if (!PhoneNumber.allNumbers(imei))
        return CheckImei.MALFORMED_IMEI;

    //obtener el ultimo digito como numero
    int last = imei.charAt(14) - 48;

    //duplicar cada segundo digito
    //sumar cada uno de los digitos resultantes del nuevo imei
    int curr;
    int sum = 0;
    for (int i = 0; i < 14; i++) {
        curr = imei.charAt(i) - 48;
        if (i % 2 != 0){
            // sum += duplicateAndSum(curr);
            // initial code from Osvel Alvarez Jacomino contains 'duplicateAndSum' method.
            // replacing it with the implementation down here:
            curr = 2 * curr;
            if(curr > 9) {
                curr = (curr / 10) + (curr - 10);
            }
            sum += curr;
        }
        else {
            sum += curr;
        }

    }

    //redondear al multiplo de 10 superior mas cercano
    int round = sum % 10 == 0 ? sum : ((sum / 10 + 1) * 10);

    return (round - sum == last) ? CheckImei.VALID_IMEI_NO_NETWORK : CheckImei.INVALID_IMEI;

}

回答by Istvan Darvas

IMEI can start with 0 digit. This is why the function input is string. Thanks for the method @KarlNicol

IMEI 可以从 0 位数字开始。这就是函数输入是字符串的原因。感谢@KarlNicol的方法

Golang

高朗

func IsValid(imei string) bool {
    digits := strings.Split(imei, "")

    numOfDigits := len(digits)

    if numOfDigits != 15 {
        return false
    }

    checkingDigit, err := strconv.ParseInt(digits[numOfDigits-1], 10, 8)
    if err != nil {
        return false
    }

    checkSum := int64(0)
    for i := 0; i < numOfDigits-1; i++ { // we dont need the last one
        convertedDigit := ""

        if (i+1)%2 == 0 {
            d, err := strconv.ParseInt(digits[i], 10, 8)
            if err != nil {
                return false
            }
            convertedDigit = strconv.FormatInt(2*d, 10)
        } else {
            convertedDigit = digits[i]
        }

        convertedDigits := strings.Split(convertedDigit, "")

        for _, c := range convertedDigits {
            d, err := strconv.ParseInt(c, 10, 8)
            if err != nil {
                return false
            }
            checkSum = checkSum + d
        }

    }

    if (checkSum+checkingDigit)%10 != 0 {
        return false
    }

    return true
}

回答by Milan Mahata

I think this logic is not right because this working only for the specific IMEI no - 490154203237518 not for other IMEI no ...I implement the code also...

我认为这种逻辑是不对的,因为这仅适用于特定的 IMEI 号 - 490154203237518 不适用于其他 IMEI 号……我也实现了代码……

var number = 490154203237518;
var array1 = new Array();
var array2 = new Array();
var specialno = 0 ;
var sum = 0 ;
var finalsum = 0;
var cast = number.toString(10).split('');
var finalnumber = '';
if(cast.length == 15){
    for(var i=0,n = cast.length; i<n; i++){

        if(i !== 14){
          if(i == 0 || i%2 == 0 ){
            array1[i] = cast[i];
          }else{
            array1[i] = cast[i]*2;
          }
        }else{
           specialno = cast[14];
        }

     }

     for(var j=0,m = array1.length; j<m; j++){
        finalnumber = finalnumber.concat(array1[j]);
     }

     while(finalnumber){
        finalsum += finalnumber % 10;
        finalnumber = Math.floor(finalnumber / 10);
     }

    contno = (finalsum/10);
    finalcontno = Math.round(contno)+1;

    check_specialno = (finalcontno*10) - finalsum; 

    if(check_specialno == specialno){
        alert('Imei')
    }else{
        alert('Not IMEI');
    }
}else{
    alert('NOT imei - length not matching');
}   


 //alert(sum);

回答by roelofs

I don't believe there are any built-in ways to authenticate an IMEI number. You would need to verify against a third party database (googling suggests there are a number of such services, but presumably they also get their information from more centralised sources).

我不相信有任何内置方法可以验证 IMEI 号码。您需要对照第三方数据库进行验证(谷歌搜索表明有许多此类服务,但据推测它们也从更集中的来源获取信息)。