在下面的程序中,我试图将十六进制值传递给CRC16函数并对其进行一些计算。但是在该行中(cc = 0xff&* ptr;)而不是对ptr的整个值进行“与”运算,仅使用第一个值。谁能指出我在这里犯的错误。 谢谢。
unsigned int crcValues[256];
unsigned int CRC16(unsigned int crc ,unsigned char *ptr)
{
unsigned char tmp;
unsigned char cc;
cc = 0xff & ((unsigned int)*ptr);
tmp = ((crc >> 8) ^ cc);
crc = (crc << 8) ^ crcValues[tmp & 0xff];
crc = crc & 0xffff;
return crc;
}
void CRCCorrectionCode()
{
unsigned int crc;
int i = 0;
int j;
for(int c = 0;c < 256; c++)
{
crc = 0;
j = c;
c = c << 8;
for(int j = 0;j < 8;j++){
if((crc ^ c) & 0x8000)
crc = (crc << 1) ^ poly;
else
crc = crc << 1;
c = c << 1;
}
c = j;
crcValues[i] = crc;
i++;
}
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
CRCCorrectionCode();
QString tmpString = QString("0x%1").arg(254, 2, 16, QLatin1Char('0'));;
int crc = 0;
CRC16(crc,(unsigned char* )tmpString.toLatin1().data());
}