I am trying to create CRC-16 using C #. The hardware I use for RS232 requires the input string to be HEX. The screenshot below shows the correct conversion. For the test, I need 8000 to be 0xC061, however the C # method that generates CRC-16 should be able to convert any given HEX string.

I tried using Nito.KitchenSink.CRC
I also tried the following which generates 8009 when 8000 is entered -
public string CalcCRC16(string strInput) { ushort crc = 0x0000; byte[] data = GetBytesFromHexString(strInput); for (int i = 0; i < data.Length; i++) { crc ^= (ushort)(data[i] << 8); for (int j = 0; j < 8; j++) { if ((crc & 0x8000) > 0) crc = (ushort)((crc << 1) ^ 0x8005); else crc <<= 1; } } return crc.ToString("X4"); } public Byte[] GetBytesFromHexString(string strInput) { Byte[] bytArOutput = new Byte[] { }; if (!string.IsNullOrEmpty(strInput) && 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; }
c # crc crc16
dynamicuser
source share