18 ID number verification methods for China’s residents, Java algorithm implementation

    public static boolean validate18Idcard(String idcard){
        if(idcard == null ) {
            return false;
        }
        if(idcard.length()!=18) {
            return false;
        }
        char [] id =idcard.tocharArray();
        int i,sum,n;
        for (sum = i = 0; i < 17; i++){
            sum += ((1 << (17 - i)) % 11) * (id[i] - '0');
        }
        n = (12 - (sum % 11)) % 11;
        if (n < 10) {
            return (n == id[17] - '0');
        } else {
            return (id[17] == 'X');
        }
    }

For more information, see Wikipedia: https://zh.wikipedia.org/wiki/ ID number of the people's Republic of China

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>