我正在寻求实现CRC-8校验和 - 一般来说,在读取CRC时,我遇到了CCITT-16的这个算法(多项式X ^ 16 + X ^ 12 + X ^ 5 + 1):< / p>
unsigned char ser_data;
static unsigned int crc;
crc = (unsigned char)(crc >> 8) | (crc << 8);
crc ^= ser_data;
crc ^= (unsigned char)(crc & 0xff) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
或者作为宏:
#define crc16(chk, byte) \
{ \
chk = (unsigned char) (chk >> 8) | (chk << 8); \
chk ^= byte; \
chk ^= (unsigned char)(chk & 0xFF) >> 4; \
chk ^= (chk << 8) << 4; \
chk ^= ((chk & 0xFF) << 4) << 1; \
}
我在这里有两个问题:
答案 0 :(得分:3)
答案 1 :(得分:0)
这是CRC8-CCITT的C实现,松散地基于这个答案的代码(https://stackoverflow.com/a/15171925/1628701):
uint8_t crc8_ccitt(uint8_t crc, const uint8_t *data, size_t dataLength){
static const uint8_t POLY = 0x07;
const uint8_t *end = data + dataLength;
while(data < end){
crc ^= *data++;
crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
crc = crc & 0x80 ? (crc << 1) ^ POLY : crc << 1;
}
return crc;
}
答案 2 :(得分:0)
此网页:https://decibel.ni.com/content/docs/DOC-11072包含指向每个常见(包括您要求的)crc计算算法的.zip文件的链接。