Last 1 bit calculation method

Many people say the last one is random, actually otherwise, is also calculated, as long as the user is given the previous 17, and then we produced crackling, a piece of addition, subtraction, multiplication, and division can get the correct last, some software will check the last one is correct, and some simple regular treatment, only this, of course, not line, program the ape to rigorous!!!!! Careful!!!!! .

It can be divided into the following steps:

  1. Multiply the first 17 digits of the id card number by a different number, and this number is specified, from the first to the 17th digits are: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 and add the results.
  2. You divide the remainder by 11, and then you have the remainder, which is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, and then you have a mapping, where the last digit of each of these numbers is 1, 0, X, 9, 8, 7, 6, 5, 4, 3, 2, if the remainder is 1, So the last digit of the ID card is 0.

The last digit of my ID card is 8. Call the calculation method and pass in the first 17 to get the last digit.

public class IdMain {
    static Map<String, String> map = new HashMap<>();
    static {
        map.put("0"."1");
        map.put("1"."0");
        map.put("2"."X");
        map.put("3"."9");
        map.put("4"."8");
        map.put("5"."Seven");
        map.put("6"."6");
        map.put("Seven"."5");
        map.put("8"."4");
        map.put("9"."3");
        map.put("10"."2");
    }
    public static void main(String[] args) throws IOException, NoSuchFieldException {
        System.out.println(calculation("xxxxxxx"));
    }
    private static String calculation(String idStr) {
        long[] x = {7.9.10.5.8.4.2.1.6.3.7.9.10.5.8.4.2};
        long total = 0;
        for (int i = 0; i < idStr.length(); i++) {
            Integer integer = Integer.valueOf(idStr.charAt(i) + "");
            total += integer * x[i];
        }
        return map.get("" + total % 11); }}Copy the code