110,534
社区成员
发帖
与我相关
我的任务
分享
static void Main(string[] args)
{
byte[] data = { 0x01, 0x03, 0x04, 0x44, 0x99, 0x9E, 0x14 };
ushort crc = crc_16(data);
Console.WriteLine("crc=0x{0:x}", crc);
//输出结果
//crc=0x8356
}
static ushort crc_16(byte[] data)
{
uint IX, IY;
ushort crc = 0xFFFF;//set all 1
int len = data.Length;
if (len <= 0)
crc = 0;
else
{
len--;
for (IX = 0; IX <= len; IX++)
{
crc = (ushort)(crc ^ (data[IX]));
for (IY = 0; IY <= 7; IY++)
{
if ((crc & 1) != 0)
crc = (ushort)((crc >> 1) ^ 0xA001);
else
crc = (ushort)(crc >> 1); //
}
}
}
byte buf1 = (byte)((crc & 0xff00) >> 8);//高位置
byte buf2 = (byte)(crc & 0x00ff); //低位置
crc = (ushort)(buf1 << 8);
crc += buf2;
return crc;
}