主要是看到了别人的算法:http://blog.jobbole.com/111556/
我自己写了个python的代码实现,感谢大侠去研究这样的算法
-
def transIDCard15To18(IdCardNo):
-
ll=[]
-
if IdCardNo is not None and len(IdCardNo.strip())==15:
-
IdCardStr=IdCardNo.strip()
-
for i in IdCardStr:
-
ll.append(i)
-
ll.insert(6,'9')
-
ll.insert(6,'1')
-
ll.append(transCardLastNo(ll))
-
return "".join(ll)
-
-
-
-
def transCardLastNo(IDCardList):
-
co=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
-
varCode=['1','0','X','9','8','7','6','5','4','3','2']
-
result=0
-
for i in range(len(IDCardList)):
-
result+=int(IDCardList[i]) * co[i]
-
-
residue=result%11
-
return varCode[residue]
-
-
-
-
print(transIDCard15To18("370986890623212"))
-
print(transIDCard15To18("370725881105149"))
结果如下:
-
./id15to18.py
-
370986198906232123
-
37072519881105149X
阅读(2050) | 评论(0) | 转发(0) |