public string CRC16(string s)
{
var crc = 0xFFFF;
var polynomial = 0x1021;
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
foreach (byte b in bytes) {
for (var i = 0; i < 8; i++) {
bool bit = ((b >> (7-i) & 1) == 1);
bool c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit)
crc ^= polynomial;
}
}
crc &= 0xffff;
return crc.ToString("X").PadLeft(4, '0');
}