I have string: 02 03 04 50 00 01
. I need to calculate the CRC for this string.
I have a function that counts a CRC:
public static UInt16 ModRTU_CRC(ushort[] buf, int len)
{
UInt16 crc = 0xFFFF;
for (int pos = 0; pos < len; pos++)
{
crc ^= (UInt16)buf[pos];
for (int i = 8; i != 0; i--)
{
if ((crc & 0x0001) != 0)
{
crc >>= 1;
crc ^= 0xA001;
}
else
crc >>= 1;
}
}
return crc;
}
I want to cast a string to an array of ushort:
ushort[] result = cmd.Split(' ').Select(item => Convert.ToUInt16(item, 16)).ToArray();
but such an array is returned to me: 2 3 4 80 0 1
.
Please tell me what should I do in order to correctly calculate the CRC.