static unsigned short crc16_update(unsigned short crc, unsigned char a)
{
int i;
crc ^= a;
for (i = 0; i < 8; ++i)
{
if (crc & 1)
{
crc = (crc >> 1) ^ 0xA001;
}
else
{
crc = (crc >> 1);
}
}
return crc;
}
static unsigned short getChk(unsigned char * b,int len)
{
unsigned short crc=0xffff;
while(len--)
{
crc=crc16_update(crc,*b++);
}
return crc;
}
1 public static ushort crc16_update(ushort crc, byte a) 2 { 3 crc ^= (ushort)a; 4 for (int i = 0; i < 8; i++) 5 { 6 if ((crc & 1) != 0) 7 { 8 crc = (ushort)(crc >> 1 ^ 0xA001); 9 } 10 else 11 { 12 crc = (ushort)(crc >> 1); 13 } 14 } 15 return crc; 16 } 17 18 public unsafe static ushort getChk(byte* b, int len) 19 { 20 ushort crc = 0xffff; 21 while (len != 0) 22 { 23 len--; 24 crc = crc16_update(crc, *b); 25 b++; 26 } 27 return crc; 28 }
项目的生成选项里的允许不安全代码要勾选上
其实完全可以直接建个c++类库项目供C#代码引用