zoukankan      html  css  js  c++  java
  • 实现身份证的15位转18位 简单飞扬

    #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");
        }
  • 相关阅读:
    mssqlserver字符串日期互相转换
    使用TripleDES算法加密/解密
    记录google,yahoo,bing爬虫记录的插件
    C#中编写sqlserver中自定义函数,实现复杂报表
    最基本的Socket编程 C#版
    基于.net平台的web框架搭建
    未来五年程序员需要掌握的10项技能
    一段输入框控制代码,包含所有控制条件!
    C#多线程编程实例编程
    C# WinForm开发系列 Socket/WCF/Rometing/Web Services
  • 原文地址:https://www.cnblogs.com/jiandanfy/p/1056607.html
Copyright © 2011-2022 走看看