Vin校验

  private static Dictionary<char, int> kvDic = new Dictionary<char, int>{
            {'0', 0},
            {'1', 1},
            {'2', 2},
            {'3', 3},
            {'4', 4},
            {'5', 5},
            {'6', 6},
            {'7', 7},
            {'8', 8},
            {'9', 9},
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            {'f', 6},
            {'g', 7},
            {'h', 8},
            {'j', 1},
            {'k', 2},
            {'l', 3},
            {'m', 4},
            {'n', 5},
            {'p', 7},
            {'q', 8},
            {'r', 9},
            {'s', 2},
            {'t', 3},
            {'u', 4},
            {'v', 5},
            {'w', 6},
            {'x', 7},
            {'y', 8},
            {'z', 9}
        };

        private static int[] wvArr = { 8, 7, 6, 5, 4, 3, 2, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

        //校验车架号
        public static bool CheckVinT(string vin)
        {
            if (vin == null || vin.Length != 17) return false;
            vin = vin.ToLower();
            int code;
            if (!int.TryParse(vin.Substring(8, 1).Replace("x", "10"), out code)) return false;
            try
            {
                return code == vin.Remove(8, 1).Select((p, i) => kvDic[p] * wvArr[i]).Sum() % 11;
            }
            catch
            {
                //vin中如果出现不包含在kvDic键中的字符时,返回false
                return false;
            }
        }

posted @ 2019-06-28 17:24  沈三公子  阅读(298)  评论(0)    收藏  举报