예제 #1
0
        private void btnComputeCrc_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            txtOutput.Text   = Arinc702Crc.AppendCrcToMessage(txtInput.Text);
            txtCrcValue.Text = txtOutput.Text.Substring(txtOutput.Text.Length - 4);
            sw.Stop();
            lblElapsedTime.Text = "Elapsed time: " + sw.Elapsed.ToString();
        }
예제 #2
0
파일: CRCTester.cs 프로젝트: davedish/CRC
        private void txtInput_TextChanged(object sender, EventArgs e)
        {
            StringBuilder sbHex         = new StringBuilder();
            StringBuilder sbBin         = new StringBuilder();
            StringBuilder sbBinNoSpaces = new StringBuilder();
            List <byte>   bytes         = new List <byte>();

            // transpose text for ARINC 702A
            txtTransposed.Text = new string(txtInput.Text.Reverse().ToArray());
            txtDenomBits.Text  = txtDenominator.Text.Length.ToString();
            if (txtDenominator.Text.Length > 0)
            {
                txtDenominatorExponents.Text = Arinc702Crc.GetBinaryExponentsString(txtDenominator.Text);
            }

            foreach (char c in txtTransposed.Text)
            {
                sbHex.Append(Convert.ToByte(c).ToString("X"));
                sbHex.Append(" ");
                sbBin.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
                sbBinNoSpaces.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
                //sbBin.Append(" ");
                bytes.Add(Convert.ToByte(c));
            }
            txtHex.Text = sbHex.ToString();
            txtBin.Text = sbBin.ToString();
            txtTransposedBinary.Text = new string(sbBinNoSpaces.ToString().Reverse().ToArray());
            txtExponents.Text        = Arinc702Crc.GetBinaryExponentsString(txtTransposedBinary.Text);

            TextBox tb = sender as TextBox;

            if (tb != null && tb.Name == "txtInput")
            {
                // numerator = 1's compliment first 16 bits of transposed binary shifted left 16 bits
                StringBuilder sbNumerator = new StringBuilder();
                for (int i = 0; i < Math.Min(16, txtTransposedBinary.Text.Length); i++)
                {
                    char c = txtTransposedBinary.Text[i];
                    if (c == '1')
                    {
                        sbNumerator.Append('0');
                    }
                    else
                    {
                        sbNumerator.Append('1');
                    }
                }
                for (int i = 16; i < txtTransposedBinary.Text.Length; i++)
                {
                    sbNumerator.Append(txtTransposedBinary.Text[i]);
                }

                sbNumerator.Append("0000000000000000");
                txtNumerator.Text = sbNumerator.ToString();
            }

            //// denominator = 10001000000100001 (16 12 5 1) -- this is actually a 17-bit CRC?
            //txtDenominator.Text = "10001000000100001";
            if (txtDenominator.Text.Length < 1)
            {
                txtDenominator.Text = "10001000000100001";
            }
            if (txtDenominator.Text.Length < 2 || txtNumerator.Text.Length < 2)
            {
                return; // don't keep going with a 1 bit denominator
            }
            // 1. left shift denominator to same digits as numerator
            // 2. xor numerator and denominator
            // 3. next line is next numerator
            // 4. when can't do it anymore, you have the remainder

            txtLongDiv.Text = "";
            string thisNumerator = txtNumerator.Text;
            string thisDenominator;
            string thisRemainder = thisNumerator;

            if (thisNumerator[0] == '0')
            {
                thisDenominator = "0".PadRight(txtNumerator.Text.Length, '0');
            }
            else
            {
                thisDenominator = txtDenominator.Text.PadRight(txtNumerator.Text.Length, '0');
            }

            while (thisNumerator.Length >= txtDenominator.Text.Length)
            {
                thisRemainder = Arinc702Crc.BinaryStringXOR(thisNumerator, thisDenominator);
                txtLongDiv.AppendText("N: " + thisNumerator + "\n");
                txtLongDiv.AppendText("D: " + thisDenominator + "\n");
                txtLongDiv.AppendText("R: " + thisRemainder + "\r\n\r\n");

                thisNumerator = thisRemainder.Substring(1); // throw away leading zero
                if (thisNumerator.Length > 0 && thisNumerator[0] == '0')
                {
                    thisDenominator = "0".PadRight(Math.Max(thisNumerator.Length, txtDenominator.Text.Length), '0');
                }
                else
                {
                    thisDenominator = txtDenominator.Text.PadRight(thisNumerator.Length, '0');
                }
            }

            txtRemainder.Text = thisNumerator; // the last remainder minus the leading zero is the final value
            StringBuilder sbCrc1 = new StringBuilder();

            txtCrc1.Text          = Arinc702Crc.BinaryStringInvert(txtRemainder.Text);
            txtTransposedCrc.Text = Arinc702Crc.BinaryStringTranspose(txtCrc1.Text);
            txtHexCrc.Text        = Arinc702Crc.BinaryStringToHexCharacterVals(txtTransposedCrc.Text);
            txtMsgBackwards.Text  = txtHexCrc.Text + txtTransposed.Text;
            txtFinalMsg.Text      = new string(txtMsgBackwards.Text.Reverse().ToArray());
            txtClassTest.Text     = Arinc702Crc.AppendCrcToMessage(txtInput.Text);
        }
예제 #3
0
        public static string AppendCrcToMessage(string input)
        {
            StringBuilder sbHex         = new StringBuilder();
            StringBuilder sbBin         = new StringBuilder();
            StringBuilder sbBinNoSpaces = new StringBuilder();
            List <byte>   bytes         = new List <byte>();

            // transpose text for ARINC 702A
            string Transposed = new string(input.Reverse().ToArray());

            //txtDenomBits.Text = binDenominator.Length.ToString();
            //if (binDenominator.Length > 0)
            //    txtDenominatorExponents.Text = Arinc702Crc.GetBinaryExponentsString(binDenominator);

            foreach (char c in Transposed)
            {
                //sbHex.Append(Convert.ToByte(c).ToString("X"));
                //sbHex.Append(" ");
                sbBin.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
                //sbBinNoSpaces.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
                //sbBin.Append(" ");
                //bytes.Add(Convert.ToByte(c));
            }
            //txtHex.Text = sbHex.ToString();
            //Binary = sbBin.ToString();
            string binTransposed = new string(sbBin.ToString().Reverse().ToArray());
            //txtExponents.Text = Arinc702Crc.GetBinaryExponentsString(binTransposed);

            //TextBox tb = sender as TextBox;
            //if (tb != null && tb.Name == "txtInput")
            //{
            // numerator = 1's compliment first 16 bits of transposed binary shifted left 16 bits
            StringBuilder sbNumerator = new StringBuilder();

            for (int i = 0; i < Math.Min(16, binTransposed.Length); i++)
            {
                char c = binTransposed[i];
                if (c == '1')
                {
                    sbNumerator.Append('0');
                }
                else
                {
                    sbNumerator.Append('1');
                }
            }
            for (int i = 16; i < binTransposed.Length; i++)
            {
                sbNumerator.Append(binTransposed[i]);
            }

            sbNumerator.Append("0000000000000000");     // here's the left shift 16 bits
            string binNumerator = sbNumerator.ToString();
            //}

            // denominator = 10001000000100001 (16 12 5 1) -- the CRC generator polynomial
            string binDenominator = "10001000000100001";
            //if (binDenominator.Length < 2 || binNumerator.Length < 2)
            //    return; // don't keep going with a 1 bit denominator

            // 1. left shift denominator to same digits as numerator
            // 2. xor numerator and denominator
            // 3. next line is next numerator
            // 4. when can't do it anymore, you have the remainder

            //txtLongDiv.Text = "";
            string binThisNumerator = binNumerator;
            string binThisDenominator;
            string binThisRemainder = binThisNumerator;

            if (binThisNumerator[0] == '0')
            {
                binThisDenominator = "0".PadRight(binNumerator.Length, '0');
            }
            else
            {
                binThisDenominator = binDenominator.PadRight(binNumerator.Length, '0');
            }

            while (binThisNumerator.Length >= binDenominator.Length)
            {
                binThisRemainder = Arinc702Crc.BinaryStringXOR(binThisNumerator, binThisDenominator);
                //txtLongDiv.AppendText("N: " + binThisNumerator + "\n");
                //txtLongDiv.AppendText("D: " + binThisDenominator + "\n");
                //txtLongDiv.AppendText("R: " + binThisRemainder + "\r\n\r\n");

                binThisNumerator = binThisRemainder.Substring(1); // throw away leading zero
                if (binThisNumerator.Length > 0 && binThisNumerator[0] == '0')
                {
                    binThisDenominator = "0".PadRight(Math.Max(binThisNumerator.Length, binDenominator.Length), '0');
                }
                else
                {
                    binThisDenominator = binDenominator.PadRight(binThisNumerator.Length, '0');
                }
            }

            string binRemainder = binThisNumerator; // the last remainder minus the leading zero is the final value
            //StringBuilder sbCrc1 = new StringBuilder();
            string binRemainderInverted = Arinc702Crc.BinaryStringInvert(binRemainder);
            string binCrc           = Arinc702Crc.BinaryStringTranspose(binRemainderInverted);
            string txtHexCrc        = Arinc702Crc.BinaryStringToHexCharacterVals(binCrc);
            string txtMsgTransposed = txtHexCrc + Transposed;

            return(new string(txtMsgTransposed.Reverse().ToArray()));
        }