title: How to generate a CRC-16 from C#
body: <p>I am trying to generate a CRC-16 using C#. The hardware I am using for RS232 requires the input string to be HEX. The screenshot below shows the correct conversion, For a test, I  need 8000 to be 0xC061, however the C# method that generates CRC-16 must be able to convert any given HEX string.</p>
  
  <p><img src="https://i.stack.imgur.com/VPTPm.png" alt="Screenshot of required output."></p>
  
  <p>I have tried using Nito.KitchenSink.CRC </p>
  
  <p>I have also tried the below which generates 8009 when 8000 is inputted - </p>
  
  <pre><code>public string CalcCRC16(string strInput)
      {
          ushort crc = 0x0000;
          byte[] data = GetBytesFromHexString(strInput);
          for (int i = 0; i &lt; data.Length; i++)
          {
              crc ^= (ushort)(data[i] &lt;&lt; 8);
              for (int j = 0; j &lt; 8; j++)
              {
                  if ((crc &amp; 0x8000) &gt; 0)
                      crc = (ushort)((crc &lt;&lt; 1) ^ 0x8005);
                  else
                      crc &lt;&lt;= 1;
              }
          }
          return crc.ToString("X4");
      }
  
      public Byte[] GetBytesFromHexString(string strInput)
      {
          Byte[] bytArOutput = new Byte[] { };
          if (!string.IsNullOrEmpty(strInput) &amp;&amp; strInput.Length % 2 == 0)
          {
              SoapHexBinary hexBinary = null;
              try
              {
                  hexBinary = SoapHexBinary.Parse(strInput);
                  if (hexBinary != null)
                  {
                      bytArOutput = hexBinary.Value;
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.Message);
              }
          }
          return bytArOutput;
      }
  </code></pre>