CRI Radio

Apr 26, 2007

15位身份证转换18位算法

#include
#include

/**************************************************************************

中华人民共和国公民身份号码升位实现

根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,
公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。排
列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺
序码和一位数字校验码。

原有的15位号码身份证的含义:从左至右前6位表示行政区号码,第7至12位为
出生日期码,第13至15为分配顺序码。新制作的18位身份证证则是在前者的基
础上,增设了两位数的世纪码和1个校验码,它们分别在第7、8和第18位上表示。


地址码表示编码对象常住户口所在县(市、旗、区)的行政区划代码。生日期码
表示编码对象出生的年、月、日,其中年份用四位数字表示,年、月、日之间
不用分隔符。顺序码表示同一地址码所标识的区域范围内,对同年、月、日出
生的人员编定的顺序号。顺序码的奇数分给男性,偶数分给女性。校验码是根
据前面十七位数字码,按照ISO 7064:1983.MOD 11-2校验码计算出来的检验码。
下面举例说明该计算方法。

某男性公民身份号码本体码为34052419800101001,首先按照公式⑴计算:

∑(ai×Wi)(mod 11)……………………………………(1)

公式(1)中:
i----表示号码字符从由至左包括校验码在内的位置序号;
ai----表示第i位置上的号码字符值;
Wi----示第i位置上的加权因子,其数值依据公司Wi=2(n-1)(mod 11)计算得出。

i 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

ai 3 4 0 5 2 4 1 9 8 0 0 1 0 1 0 0 1 a1

Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1

ai×Wi 21 36 0 25 16 16 2 9 48 0 0 9 0 5 0 0 2 a1

根据公式(1)进行计算:

∑(ai×Wi) =(21+36+0+25+16+16+2+9+48++0+0+9+0+5+0+0+2) = 189

189 ÷ 11 = 17 + 2/11

∑(ai×Wi)(mod 11) = 2

然后根据计算的结果,从下面的表中查出相应的校验码,其中X表示计算结果为10:

∑(ai×WI)(mod 11) 0 1 2 3 4 5 6 7 8 9 10
校验码字符值ai 1 0 X 9 8 7 6 5 4 3 2
根据上表,查出计算结果为2的校验码为所以该人员的公民身份号码应该为

34052419800101001X。

Copyright (c) 2001-2003 Yacht Yang


This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**************************************************************************/

static const char version[] = "0.30";

int main(void)
{
int id[18];
int mod[18] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2,1};
char crc[11] = {'1','0','x','9','8','7','6','5','4','3','2'};
long count;
int i;

printf("\n\t 中华人民共和国公民身份号码升位实现\n\n");
printf("\n\t根据〖中华人民共和国国家标准 GB 11643-1999〗中有关公民身份号码的规定,");
printf("\n\t公民身份号码是特征组合码,由十七位数字本体码和一位数字校验码组成。排");
printf("\n\t列顺序从左至右依次为:六位数字地址码,八位数字出生日期码,三位数字顺");
printf("\n\t序码和一位数字校验码。");
printf("\n\t");
printf("\n\t原有的15位号码身份证的含义:从左至右前6位表示行政区号码,第7至12位为");
printf("\n\t出生日期码,第13至15为分配顺序码。新制作的18位身份证证则是在前者的基");
printf("\n\t础上,增设了两位数的世纪码和1个校验码,它们分别在第7、8和第18位上表示。");
printf("\n\n");

printf("\n\tThe code only for 1900-1999:");
printf("\n\tInput id card 15bits:\n\t");
for(i = 0;i < 6;i++)
scanf("%01d",&id[i]);

id[6]=1;
id[7]=9;

for(i = 8;i < 17;i++)
scanf("%01d",&id[i]);

for(count=0,i = 0;i < 17;i++)
count += id[i] * mod[i];

id[i] = crc[count%11];

printf("\n\tnow answer for u:\n\t\t");
printf("last id = %c\n\t",crc[count%11]);
if(id[16]%2 == 0)
printf("Lady: ");
else
printf("Gentleman: ");

printf("ur id is:\n\t");

for(i = 0;i < 17;i++)
printf("%01d",id[i]);

printf("%c\n",id[i]);

printf("\n\tthank u using,any qustion mailto:\n\t!\n\n\t");

getch();

return 0;
}