组织机构代码校验码 验证程序

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace org_bm_checker

{

    class Program

    {

        static void Main(string[] args)

        {

            org_bm_class op = new org_bm_class();

            string y_bm = "PDY00002";

            //y_bm = "71774346-9";

            //y_bm = "73767624-0";

            //y_bm = "74549579-X";

            y_bm = "74610229-x";



            Console.WriteLine(y_bm);

            string bm = op.Code9(y_bm);

            Console.WriteLine(bm);

            string t=Console.ReadLine();

        }

    }

    //组织机构代码校验码   验证程序

    //组织机构代码71774346-9,73767624-0

    //规定本体代码PDY00001至PDY99999为自定义区,供各系统编制内部组织机构代码使用。

    //自定义区内编制的组织机构代码不作为个系统之间信息交换的依据

    //组织机构代码管理办法  编制规则

    //校验码按照以下公式计算:

    /*C9=11-MOD(∑Ci(i=1→8)×Wi,11)

    式中: MOD——代表求余函数;

    i——代表代码字符从左至右位置序号;

    Ci——代表第i位上的代码字符的值(具体代码字符见附表);

    C9——代表校验码;

    Wi——代表第i位上的加权因子,其数值见下表:

     i=1,2,3,4,5,6,7,8

    Wi=3,7,9,10,5,8,4,2  

    当C9的值为10时,校验码应用大写的拉丁字母X表示;当C9的值为11时校验码用0表示。

     */

    //代码字符机器处理字符数值

    // 0 0   1 1  9 9 A 10 B 11  Z 35

    public class org_bm_class

    {

        public string Code9(string str_in)

        {

            string rbc = "";

            //W权位

            int[] wArray =new int[]{3,7,9,10,5,8,4,2};            

            string str_in_U = str_in.ToUpper();

            char c;

            int zz=0, z=0;

            for (int i =0; i <=7; i++)

            {

                c = char.Parse(str_in.Substring(i, 1));

                if (c >= 'A' && c <= 'Z')  //A-Z字符

                {

                    z = ((int)c - 55) * wArray[i];

                }

                else if (c >= '0' && c <= '9')  //0-9字符

                {

                    z = int.Parse(c.ToString()) * wArray[i];

                }

                else

                {

                    rbc = "Err不能输入其他字符错误码";

                    return rbc;

                }

                zz += z;

            }

            string C9 = "";

            int jav = 11 - (zz % 11);

            if (jav == 10)

            {

                C9 = "X";

            }

            else if (jav == 11)

            {

                C9 = "0";

            }

            else

            {

                C9 = jav.ToString().Trim(); //删除文本前导空格

            }

            rbc = str_in.Substring(0, 8) +"-"+C9;

            return rbc;

        }

    }

}


你可能感兴趣的:(代码)