Implementing CRC-4 in C #

I was looking for a network to implement C # 4-bit cyclic redundancy check (CRC-4-ITU), but so far I have not been successful.

Is there anyone who can give me a reference implementation of CRC-4-ITU? Preferably with a standard polynomial, if there is a standard polynomial (I read the specification pointed to by wikipedia as the CRC4 specification without finding the definition of the polynomial).

I also really appreciate some kind of test suite or test data to test CRC4 implementation.

Thank!

+3
source share
1 answer

Cyclic Redundancy Check Wikipedia , x ^ 4 + x + 1. .

CRC16. , , , 4 .

   public ushort calculate(byte[] bytes)
    {
        int crc = 0xFFFF; // initial value
        // loop, calculating CRC for each byte of the string
        for (int byteIndex = 0; byteIndex < bytes.Length; byteIndex++)
        {
            ushort bit = 0x80; // initialize bit currently being tested
            for (int bitIndex = 0; bitIndex < 8; bitIndex++)
            {
                bool xorFlag = ((crc & 0x8000) == 0x8000);
                crc <<= 1;
                if (((bytes[byteIndex] & bit) ^ (ushort)0xff) != (ushort)0xff)
                {
                    crc = crc + 1;
                }
                if (xorFlag)
                {
                    crc = crc ^ 0x1021;
                }
                bit >>= 1;
            }
        }
        return (ushort)crc;
    }

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24775723.html

, :

http://www.ross.net/crc/download/crc_v3.txt

", CRC-, , .

+2

Source: https://habr.com/ru/post/1688277/


All Articles