CRC-8 的实施。初始化参数有什么作用?
Implementation of CRC-8. What does the init parameter do?
我想为不同标准的 CRC 编写程序(位处理)卡在 init 参数上。当 init = 0x00 时,它可以正常工作,不仅适用于 CRC-8 ... 但是一旦您更改 init,值就不会正确给出。问题是什么 ? init只需要改变寄存器的初始值?
CRC-8 / init = 0x00, poly = 0x07 - 工作正常
CRC-8 CDMA / init = 0xFF, poly = 0x9b - aldeady no
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();
}
操作顺序错误。左移 CRC 的操作顺序应该是 CRC 的 MSB(最高有效位)^= 消息的 MSB,如果结果为 1,则 CRC <<= 1,CRC ^= poly,否则 CRC <<= 1。然后对消息的下一个 MSB 重复该过程,依此类推。
也没有显示 Get...Bits... 的位顺序。
对于第一种情况,我得到 0xC0,对于第二种情况,我得到 0x28。
代码可以通过xor'in 8 bits at time来简化:
typedef unsigned char BYTE;
BYTE gencrc1(BYTE *bfr, size_t len)
{
size_t i;
BYTE crc = 0x00;
while(len--){
crc ^= *bfr++;
for(i = 0; i < 8; i++){
if(crc & 0x80){
crc <<= 1;
crc ^= 0x07;
} else {
crc <<= 1;
}
}
}
return(crc);
}
BYTE gencrc2(BYTE *bfr, size_t len)
{
size_t i;
BYTE crc = 0xff;
while(len--){
crc ^= *bfr++;
for(i = 0; i < 8; i++){
if(crc & 0x80){
crc <<= 1;
crc ^= 0x9b;
} else {
crc <<= 1;
}
}
}
return(crc);
}
gencrc2 示例使用 "long hand division" 方法,crc poly = 0x19b = 110011011,消息 = 0x41,附加 8 个零位(用于余数)。
11011000
------------------
110011011 | 0100000100000000 0x41 with 8 zero bits
11111111 crc init value is 0xff
--------
101111100
110011011
---------
111001110
110011011
---------
010101010
000000000
---------
101010100
110011011
---------
110011110
110011011
---------
000001010
000000000
---------
000010100
000000000
---------
000101000
000000000
---------
00101000 0x28 is remainder
我想为不同标准的 CRC 编写程序(位处理)卡在 init 参数上。当 init = 0x00 时,它可以正常工作,不仅适用于 CRC-8 ... 但是一旦您更改 init,值就不会正确给出。问题是什么 ? init只需要改变寄存器的初始值?
CRC-8 / init = 0x00, poly = 0x07 - 工作正常
CRC-8 CDMA / init = 0xFF, poly = 0x9b - aldeady no
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();
}
操作顺序错误。左移 CRC 的操作顺序应该是 CRC 的 MSB(最高有效位)^= 消息的 MSB,如果结果为 1,则 CRC <<= 1,CRC ^= poly,否则 CRC <<= 1。然后对消息的下一个 MSB 重复该过程,依此类推。
也没有显示 Get...Bits... 的位顺序。
对于第一种情况,我得到 0xC0,对于第二种情况,我得到 0x28。
代码可以通过xor'in 8 bits at time来简化:
typedef unsigned char BYTE;
BYTE gencrc1(BYTE *bfr, size_t len)
{
size_t i;
BYTE crc = 0x00;
while(len--){
crc ^= *bfr++;
for(i = 0; i < 8; i++){
if(crc & 0x80){
crc <<= 1;
crc ^= 0x07;
} else {
crc <<= 1;
}
}
}
return(crc);
}
BYTE gencrc2(BYTE *bfr, size_t len)
{
size_t i;
BYTE crc = 0xff;
while(len--){
crc ^= *bfr++;
for(i = 0; i < 8; i++){
if(crc & 0x80){
crc <<= 1;
crc ^= 0x9b;
} else {
crc <<= 1;
}
}
}
return(crc);
}
gencrc2 示例使用 "long hand division" 方法,crc poly = 0x19b = 110011011,消息 = 0x41,附加 8 个零位(用于余数)。
11011000
------------------
110011011 | 0100000100000000 0x41 with 8 zero bits
11111111 crc init value is 0xff
--------
101111100
110011011
---------
111001110
110011011
---------
010101010
000000000
---------
101010100
110011011
---------
110011110
110011011
---------
000001010
000000000
---------
000010100
000000000
---------
000101000
000000000
---------
00101000 0x28 is remainder