예제 #1
0
        public void ChecksumFor255CharsAStringIsCorrect(Crc16CcittInitialValue initialValue, ushort expected)
        {
            var s     = new string('A', 256);
            var input = s.ToCharArray().Select(c => (byte)c).ToArray();

            testChecksum(input, initialValue, expected);
        }
예제 #2
0
        private static void testChecksum(byte[] input, Crc16CcittInitialValue initialValue, ushort expected)
        {
            var checksum = new Crc16CcittChecksum(initialValue) as IChecksum;

            var crc    = checksum.Calculate(input);
            var actual = BitConverter.ToUInt16(crc, 0);

            Assert.That(actual, Is.EqualTo(expected), string.Format("expected 0x{0:X} but was 0x{1:X}", expected, actual));
        }
예제 #3
0
 public Crc16CcittChecksum(Crc16CcittInitialValue initialValue)
 {
     this.m_InitialValue = (ushort)initialValue;
     for (int i = 0; i < table.Length; ++i)
     {
         ushort temp = 0;
         var    a    = (ushort)(i << 8);
         for (int j = 0; j < 8; ++j)
         {
             if (((temp ^ a) & 0x8000) != 0)
             {
                 temp = (ushort)((temp << 1) ^ poly);
             }
             else
             {
                 temp <<= 1;
             }
             a <<= 1;
         }
         table[i] = temp;
     }
 }
예제 #4
0
        public void ChecksumForReferenceStringIsCorrect(Crc16CcittInitialValue initialValue, ushort expected)
        {
            var input = "123456789".ToCharArray().Select(c => (byte)c).ToArray();

            testChecksum(input, initialValue, expected);
        }
예제 #5
0
 public void ChecksumForCharAArratIsCorrect(Crc16CcittInitialValue initialValue, ushort expected)
 {
     byte[] input = new byte[] { (byte)'A' };
     testChecksum(input, initialValue, expected);
 }