实现身份证的15位转18位
2005-12-07 09:18
393 查看
#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"
;
}
相关文章推荐
- java代码实现15位身份证号码升级到18位
- 实现身份证的15位转18位
- Javascript实现身份证号码15位转18位的算法
- java实现身份证15位转18位
- 身份证号码15位转18位 C#实现
- JS实现身份证号码15位转18位时最后一位的算法
- VB.NET实现身份证15位升18位的算法
- 使用C#实现身份证号码15位到18位的转换
- java 实现从15位~18位的身份证号码转换,校验中国大陆公民身份证、香港居民身份证、澳门身份证和台湾身份证。
- C# 15位身份证号码转18位算法实现
- Java代码实现15位身份证补齐成18位
- Delphi实现身份证15位升18位的算法
- VB.NET实现身份证15位升18位的算法
- 用C#实现由15位身份证号升级到18位的算法!
- C#实现的18位身份证格式验证算法[转载]
- C#实现的18位身份证格式验证算法[转载] 选择自 mYang 的 Blog
- C#实现的18位身份证格式验证算法
- 将15位身份证升级成18位的用户定义函数
- 将身份证的15位号码升级为18位
- 【关键字】Javascript js 身份证号码 检测 规则 18位 15位