机动车车辆识别代码(车架号)校验函数

public static bool CheckVin(string vin)
{
    if (vin == null || vin.Length != 17) return false;
    int symbol, weight, verify, sum = 0;
    bool isRepeat = true;
    if (vin[8] >= 48 && vin[8] <= 57)
        verify = vin[8] - 48;//0到9
    else if (vin[8] == 88)
        verify = 10;//X
    else
        return false;

    for (int i = 0; i < vin.Length; i++)
    {
        if (i< 7)
            weight = 8 - i;
        else if (i > 8)
            weight = 18 - i;
        else if (i == 7)
            weight = 10;
        else
            continue;
        
        if (vin[i] >= 48 && vin[i] <= 57)
            symbol = vin[i] - 48;//0-9
        else if (vin[i] >= 65 && vin[i] <= 72)
            symbol = vin[i] - 64;//A-H
        else if (vin[i] >= 74 && vin[i] <= 82 && vin[i] != 79 && vin[i] != 81)
            symbol = vin[i] - 73;//J-R不含O,Q
        else if (vin[i] >= 83 && vin[i] <= 90)
            symbol = vin[i] - 81;//S-Z
        else
            return false;
        sum += symbol * weight;

        if (isRepeat && i > 0) isRepeat = vin[i] == vin[i - (i != 9 ? 1 : 2)];
    }

    if (isRepeat) return false;
    return verify == sum % 11;
}

 

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页