我想寫一個CRC不同標準的程序(位處理)卡在init參數上。當init = 0x00時,它可以正常工作,不僅適用於CRC-8 ...但是,只要更改init,值就不會正確發送。問題是什麼 ? Init只需要改變寄存器的初始值?CRC-8的實現。 init參數有什麼作用?
CRC-8/INIT = 0×00,聚= 0×07 - 工作正常
CRC-8 CDMA/INIT = 0xFF時,聚= 0x9b - aldeady沒有
CRC-8:
int CRC8() {
dynamic_bitset<> regix = MyCRC::GetRegixAsBits(0x00, 8); // init = 0x00
dynamic_bitset<> mess = MyCRC::GetIntAsBitset(0x41, 8); // mess = 0x41
dynamic_bitset<> poly = MyCRC::GetPolyAsBitset(8, 0x07); // poly = 0x07
cout << regix << endl; // 0000 0000 == 0x00
cout << mess << endl; // 0100 0001 0000 0000 == 0x41 + 8 нулей
cout << poly << endl; // 0000 0111 == 0x07
while (mess.size() > 0) {
if (regix[7] == 0) {
regix = regix << 1;
regix[0] = mess[mess.size() - 1];
}
else {
regix = regix << 1;
regix[0] = mess[mess.size() - 1];
regix = regix^poly;
}
mess.pop_back();
}
cout << hex << regix.to_ulong() << endl; // 1100 0000 = 0xC0 | 0xC0 (crccalc.com) OK
return regix.to_ulong();
}
CRC-8 CDMA:
int CRC8_CDMA() {
dynamic_bitset<> regix = MyCRC::GetRegixAsBits(0xFF, 8); // init = 0xFF
dynamic_bitset<> mess = MyCRC::GetIntAsBitset(0x41, 8); // mess = 0x41
dynamic_bitset<> poly = MyCRC::GetPolyAsBitset(8, 0x9b); // poly = 0x9b
cout << regix << endl; // 1111 1111 == 0xFF
cout << mess << endl; // 0100 0001 0000 0000 == 0x41 + 8 нулей
cout << poly << endl; // 1001 1011 == 0x9b
while (mess.size() > 0) {
if (regix[7] == 0) {
regix = regix << 1;
regix[0] = mess[mess.size() - 1];
}
else {
regix = regix << 1;
regix[0] = mess[mess.size() - 1];
regix = regix^poly;
}
mess.pop_back();
}
cout << hex << regix.to_ulong() << endl; // 1110 0010 = 0xE2 | 0x28 (crccalc.com) FALSE
return regix.to_ulong();
}
它設置了寄存器的初始值,因爲您已經正確地猜測了。不同的價值觀產生不同的結果並不完全是一個謎。我努力在這裏看到任何一種實際的問題。 – EJP
@EJP我在代碼註釋中添加了一個改進。我的程序的值與在線計算器不匹配... – Evgen8