Domanda

There's a similar question asked and answered in C, but I'm struggling a bit to achieve the same thing in C#.

Generator Polynomial : x^16 + x^10 + x^8 + x^7 + x^3 + 1 which is equivalent to 10000010110001001 in binary.


I have 48 bits of data, and now I required to generate 16 bits of CRC, here's the code:

  private bool[] MakeCRC(string BitString)
    {
        bool[] Res = new bool[17];  
        bool[] CRC = new bool[16];
        int i;
        bool DoInvert= false;

        for (i = 0; i < 16; ++i) // Init before calculation
            CRC[i] = false;

        for (i = 0; i < BitString.Length; ++i)
        {
            if (BitString[i] == '1')
                DoInvert = true ^ CRC[15];

            //DoInvert = ('1' == BitString[i]) ^ CRC[15]; // XOR required?

            CRC[15] = CRC[14];
            CRC[14] = CRC[13];
            CRC[13] = CRC[12];
            CRC[12] = CRC[11];
            CRC[11] = CRC[10];
            CRC[10] = CRC[9] ^ DoInvert;
            CRC[9] = CRC[8];
            CRC[8] = CRC[7] ^ DoInvert;
            CRC[7] = CRC[6] ^ DoInvert;
            CRC[6] = CRC[5];
            CRC[5] = CRC[4];
            CRC[4] = CRC[3];
            CRC[3] = CRC[2] ^ DoInvert;
            CRC[2] = CRC[1];
            CRC[1] = CRC[0];
            CRC[0] = DoInvert;
        }

        for (i = 0; i < 16; ++i)
            Res[15 - i] = CRC[i] ? true : false;

        Res[16] = false; // Set string terminator

        return (Res);

    }

but the above code, gives me wrong output, please suggest me if there's any better way of doing it.

Edit:

Data(a0 to a47): 100011100011000001000001000100001000000000001000

Polynomial: 10000010110001001

Output Obtained: 0011 0100 1111 0111

Output Expected: 1100 1101 0100 1111


Thanks for your time.!

È stato utile?

Soluzione

uncomment DoInvert = ('1' == BitString[i]) ^ CRC[15]; and remove the line above and it works.

Your replacement for the commented line is wrong. Right would be:

if (BitString[i] == '1')
    DoInvert = true ^ CRC[15];
else
    DoInvert = false ^ CRC[15];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top