标签:length amp ret code short c++ int i++ window
CRC 在线工具:http://www.ip33.com/crc.html
1.CRC16_XMODEM x16+x12+x5+1
多项式:1021
初始值:0000
C#
public static int CRC16_XMODEM(byte[] datas) { int crc = 0x0000;//初始值 int crcPoly = 0x1021; for(int i=0;i<datas.Length;i++) { crc ^= (datas[i] << 8); for(int j=0;j<8;j++) { if ((crc & 0x8000)>0) { crc = (ushort)((crc << 1) ^ crcPoly); } else { crc = (ushort)(crc << 1); } } } return crc; }
c++
uint16_t MainWindow::crc16_xmodem(uint8_t *data, int length) { uint8_t i; uint16_t crc = 0; // Initial value while(length--) { crc ^= (uint16_t)(*data++) << 8; // crc ^= (uint16_t)(*data)<<8; data++;for (i = 0; i < 8; ++i) { if ( crc & 0x8000 ) { crc = (crc << 1) ^ 0x1021; } else { crc <<= 1; } } } return crc; }
标签:length amp ret code short c++ int i++ window
原文地址:https://www.cnblogs.com/ike_li/p/9182002.html