Advertisement
Sawy3R11

CRC-16

Jan 13th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System;
  2.                    
  3. public class Program
  4. {
  5.     public static void Main()
  6.     {
  7.         Console.WriteLine("Hello World");
  8.        
  9.         byte[] arr = new byte[2];
  10.         arr[0] = 0;
  11.         arr[1] = 0xC4;
  12.        
  13.         Console.WriteLine("crc: "+Compute_CRC16_Simple(arr));
  14.     }
  15.    
  16.     public static ushort Compute_CRC16_Simple(byte[] bytes)
  17. {
  18.     const ushort generator = 0x8005;    /* divisor is 16bit */
  19.     ushort crc = 0; /* CRC value is 16bit */
  20.  
  21.     foreach (byte b in bytes)
  22.     {
  23.         crc ^= (ushort)(b << 8); /* move byte into MSB of 16bit CRC */
  24.  
  25.         for (int i = 0; i < 8; i++)
  26.         {
  27.             if ((crc & 0x8000) != 0) /* test for MSB = bit 15 */
  28.             {
  29.                 crc = (ushort)((crc << 1) ^ generator);
  30.             }
  31.             else
  32.             {
  33.                 crc <<= 1;
  34.             }
  35.             Convert.ToString(5, 2);
  36.             Console.WriteLine("crc: "+Convert.ToString(crc, 2));
  37.         }
  38.     }
  39.  
  40.     return crc;
  41. }
  42.    
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement