代码原创(个别人注意了):
#include <iostream>
using namespace std;
//实现身份证的15位转18位
void per15To18(char perIDSrc[])
{
int iS = 0;
//加权因子常数
int const iW[]={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
//校验码常数
char const LastCode[]="10X98765432";
//新身份证号
char perIDNew[19];
for( int i = 0; i < 6; i++ )
{
perIDNew[i] = perIDSrc[i];
}
//填在第6位及第7位上填上‘1’,‘9’两个数字
perIDNew[6] = '1';
perIDNew[7] = '9';
for( int i = 8; i < 17; i++ )
{
perIDNew[i] = perIDSrc[i - 2];
}
//进行加权求和
for( int i=0; i<17; i++)
{
iS += (perIDNew[i]-'0') * iW[i];
/*
对于perIDNew[i]-'0'解释一下:
perIDNew[i]->ASCII码,取得它的值实际是十进制数;
'0' ->ASCII码,同上;
perIDNew[i]-'0' -> 得到具体的十进制数值;
对于这里面的为什么会进行转换,具体去看C++PRIMER,呵呵。
*/
}
//取模运算,得到模值
int iY = iS%11;
//从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
perIDNew[17] = LastCode[iY];
//加上结束符
perIDNew[18] = '\0';
cout << "This old PerID is : " << perIDSrc << endl;
cout << "This new PerID is : " << perIDNew << endl;
}
//对身份证最后一位验证码进行校验(这个就不解释了)
void per18(char perIDSrc[])
{
int iS = 0;
int const iW[]={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char const LastCode[]="10X98765432";
for(int i=0;i<17;i++)
{
iS += (int)(perIDSrc[i]-'0') * iW[i];
}
int iY = iS%11;
cout << "The PerID is : " << perIDSrc << endl;
cout << "The Last Number is :" << LastCode[iY] << endl;
}
void main()
{
per15To18("320482810923461");
per18("410928198509022324");
system("pause");
}
using namespace std;
//实现身份证的15位转18位
void per15To18(char perIDSrc[])
{
int iS = 0;
//加权因子常数
int const iW[]={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
//校验码常数
char const LastCode[]="10X98765432";
//新身份证号
char perIDNew[19];
for( int i = 0; i < 6; i++ )
{
perIDNew[i] = perIDSrc[i];
}
//填在第6位及第7位上填上‘1’,‘9’两个数字
perIDNew[6] = '1';
perIDNew[7] = '9';
for( int i = 8; i < 17; i++ )
{
perIDNew[i] = perIDSrc[i - 2];
}
//进行加权求和
for( int i=0; i<17; i++)
{
iS += (perIDNew[i]-'0') * iW[i];
/*
对于perIDNew[i]-'0'解释一下:
perIDNew[i]->ASCII码,取得它的值实际是十进制数;
'0' ->ASCII码,同上;
perIDNew[i]-'0' -> 得到具体的十进制数值;
对于这里面的为什么会进行转换,具体去看C++PRIMER,呵呵。
*/
}
//取模运算,得到模值
int iY = iS%11;
//从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。
perIDNew[17] = LastCode[iY];
//加上结束符
perIDNew[18] = '\0';
cout << "This old PerID is : " << perIDSrc << endl;
cout << "This new PerID is : " << perIDNew << endl;
}
//对身份证最后一位验证码进行校验(这个就不解释了)
void per18(char perIDSrc[])
{
int iS = 0;
int const iW[]={7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
char const LastCode[]="10X98765432";
for(int i=0;i<17;i++)
{
iS += (int)(perIDSrc[i]-'0') * iW[i];
}
int iY = iS%11;
cout << "The PerID is : " << perIDSrc << endl;
cout << "The Last Number is :" << LastCode[iY] << endl;
}
void main()
{
per15To18("320482810923461");
per18("410928198509022324");
system("pause");
}