中国新式身份证号的最后一位是校验码,根据前 17 位计算而得。具体算法请自行分析下面的程序,很简单的:)
有 1/11 的概率出现尾号为 X。这为身份证在计算机系统的应用以及人民群众的生活、工作带来了很多麻烦。
参考:中华人民共和国国家标准 GB11643-1999。
#!/usr/bin/python
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
"""
Given the first 17 digits, calculate the last digit of an
18-digit Chinese ID (second-generation ID) number
"""
import sys,re
pattern = re.compile(r'^\d{17}$')
err = 0
for x in sys.argv[1:]:
if not pattern.match(x):
sys.stderr.write ('Error: {0}\n'.format(x))
err = 1
continue
n = sum(map(lambda a,b:a*(ord(b)-ord('0')), \
(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2), x)) % 11
print (x+'10X98765432'[n])
sys.exit(err)