⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 crc 算法, 使用Visual studio 2005实现
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace crc
{
    class crctest
    {
        private string input_data;  //数据
        private string generator;   //生成器
        private BitArray data;      //最后生成的传输的数据

        public string Gen           //提供外界访问生成器的属性器
        {
            get { return generator; }
            set { generator = value; }
        }

        public string In_data       //外部输入数据的属性器
        {
            get { return input_data; }
            set { input_data = value; }
        }

        public BitArray Data{get { return data; }}  //传输数据的属性器

        public void generat()//生成一个等于D·2r 的位数组 
        {
            BitArray na = new BitArray(In_data.Length + Gen.Length-1);  //初始化长度
            for (int ix = 0; ix < In_data.Length; ix++)
                if (In_data[ix].Equals('1')) na[ix] = true;             //初始化值
                else na[ix] = false;            
            data = na;            
        }

        BitArray convert(string a) //把字串转换为 BitArray,以备后续之用
        {
            BitArray gen = new BitArray(a.Length);
            for (int ix = 0; ix < a.Length; ix++)
                if (a[ix].Equals('1')) gen[ix] = true;
                else gen[ix] = false;
            return gen;
        }

        public BitArray send()//处理数据,生成最后传输的结果

        {   
            generat();
            BitArray gener = new BitArray(convert(Gen));
            BitArray sub_final = new BitArray(gener.Length);

            BitArray final = new BitArray(data);
            for (int ix = 0; ix < final.Length-(gener.Length-1); ix++)//模2除法
            {
                for (int i = 0; i < gener.Length; i++) sub_final[i] = final[i + ix];
                if (sub_final[0]) sub_final.Xor(gener);
                for (int i = 0; i < gener.Length; i++) final[i + ix] = sub_final[i];
            }
            Data.Or(final);//余数存在final中,通过OR运算,把生成余数放到数据的最后
            return Data; 
        }

        public BitArray check()
        {
            BitArray check_data = new BitArray(convert(In_data));
            BitArray gener = new BitArray(convert(Gen));
            BitArray sub_ck = new BitArray(gener.Length);
            
            for (int ix = 0; ix < check_data.Length - (gener.Length - 1); ix++)//模2除
            {
                for (int i = 0; i < gener.Length; i++) sub_ck[i] = check_data[i + ix];
                if (sub_ck[0]) sub_ck.Xor(gener);
                for (int i = 0; i < gener.Length; i++) check_data[i + ix] = sub_ck[i];
            }
            return check_data;      //得到的结果如果是全0,则结果是正确的
        }

//BitArray中的值都是ture, false,不便于观察结果,所以转换成字串传输
        public override string ToString()
        {
            StringBuilder a = new StringBuilder();
            string na;
             for (int ix = 0; ix < data.Length; ix++)
                if(data[ix])a.Append('1');
                else a.Append('0');
            na = a.ToString();
            return na;
        }

        public string ToString(int last)
        {
            BitArray ck_data = send();
            StringBuilder a = new StringBuilder();
            string na;
            for (int ix = 0; ix < last; ix++)
                if (ck_data[ix]) a.Append('1');
                else a.Append('0');
            na = a.ToString();
            return na;
        }
    }

    class test 
    {
        public static void Main()
        {
            Console.Write("选择子程序:1、生成一个数据 2、检验生成的数据    ");
            int ch = Convert.ToInt16(Console.ReadLine());
            crctest nc;

            switch (ch)
            {
                case 1:
                    nc = generator();
                    BitArray send_result = nc.send();
                    Console.WriteLine("传输出去的数据是:{0}", nc.ToString());
                    break;
                case 2:
                    nc = check();
                    BitArray ck_result = nc.check();
                    bool ck = true;
                    for(int ix = 0; ix < ck_result.Length; ix ++)
                        if (ck_result[ix])
                        {
                            ck = false;
                            break;
                        }
                    //ck = true ck_result全0,数据是正确的。
                    if (ck) Console.WriteLine("接收到的数据是正确的,接收到的数据是:{0}", nc.ToString(nc.In_data.Length - (nc.Gen.Length - 1)));
                    else Console.WriteLine("抱歉,数据在传输的过程中出错!!\n接收到的错误数据是:{0}", nc.ToString(nc.In_data.Length - (nc.Gen.Length - 1)));
                    break;
            }
            Console.Write("是否继续?[Y/N]  ");
            switch (Console.ReadLine())
            {
                case "y":
                case "Y":
                    Console.WriteLine();    Main();     break;
                default: break;
            }
        }

        public static crctest generator()
        {
            crctest nc = new crctest();
            Console.Write("输入要传输的数据:");
            string input = Console.ReadLine();
            nc.In_data = input;
            Console.Write("传输使用的生成器:");
            string gen = Console.ReadLine();
            nc.Gen = gen;
            return nc;
        }

        public static crctest check()
        {
            crctest nc = new crctest();
            Console.Write("接收到的数据是:");
            string input = Console.ReadLine();
            nc.In_data = input;
            Console.Write("约定的生成器是:");
            string gen = Console.ReadLine();
            nc.Gen = gen;
            return nc;
        }
    }
}

/*
设D=110101,G=1001,即d=6, r=3。
1、求D·2r: 110101000
2、求(D·2r/G)的余数R:
       R= 011
3、加了CRC的数据:
      T= 110101011
可以验证, T能被G模二除尽。
*/

/*          for (int x = 0; x < result.Length; x++)
            {
                if (result[x]) Console.Write("1");
                else Console.Write("0");
            }
            Console.WriteLine();
            Console.WriteLine("{0}  {1}  {2}", nc.In_data.Length, nc.Gen.Length, nc.Data.Length);
 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -