Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
# encoding algorithm
CRC=0xFF
CRC_New=0x00
For i=23 to 0 {
CRC_New[7]=CRC[6]
CRC_New[6]=CRC[5]
CRC_New[5]=CRC[4]
CRC_New[4]=CRC[3] ^CRC[7]
CRC_New[3]=CRC[2] ^CRC[7]
CRC_New[2]=CRC[1] ^CRC[7]
CRC_New[1]=CRC[0]
CRC_New[0]=Input_data[i]^CRC[7]
CRC=CRC_New
}
# final invertion before appending
For i=0 to 7 {
CRC[i] = CRC[i]^1
}
The symbol ^ indicates the XOR operator.
For example, the CRC of 0xA0CA85 is 2F and the CRC of 0x3B007C is C2.


What I have tried:

CRC results do not match.
I don't think it's the general CRC table method I know.
Can anyone convert and explain to CPP code?
Posted
Updated 19-Apr-22 23:40pm
v2
Comments
CPallini 20-Apr-22 4:21am    
Convert... From?
Min woo Park 2022 21-Apr-22 2:08am    
OK, Thanks!
OriginalGriff 20-Apr-22 5:26am    
"CRC results do not match."
Do not match what? What are you testing / comparing with what, and how?

Assuming the code you posted works on bits, here you are a C++ program that closely resembles it:
C++
#include <iostream>
using namespace std;

union byte
{
  struct
  {
    unsigned b0 : 1;
    unsigned b1 : 1;
    unsigned b2 : 1;
    unsigned b3 : 1;
    unsigned b4 : 1;
    unsigned b5 : 1;
    unsigned b6 : 1;
    unsigned b7 : 1;
  } bit;
  unsigned char u8;
};

byte compute_crc(unsigned data)
{
  byte crc;
  byte crc_new;
  crc.u8 = 0xFF;
  crc_new.u8 = 0x00;

  for (size_t i=0; i<24; ++i)
  {
    crc_new.bit.b7 = crc.bit.b6;
    crc_new.bit.b6 = crc.bit.b5;
    crc_new.bit.b5 = crc.bit.b4;
    crc_new.bit.b4 = crc.bit.b3 ^ crc.bit.b7;
    crc_new.bit.b3 = crc.bit.b2 ^ crc.bit.b7;
    crc_new.bit.b2 = crc.bit.b1 ^ crc.bit.b7;
    crc_new.bit.b1 = crc.bit.b0;
    unsigned data_i = (data >> (23-i) ) & 1;
    crc_new.bit.b0 = data_i ^ crc.bit.b7;
    crc.u8 = crc_new.u8;
  }
  crc.u8 ^= 0xFF;
  return crc;
}

int main()
{
  unsigned a[] = { 0xA0CA85, 0x3B007C};

  for (size_t n = 0; n < sizeof(a)/sizeof(a[0]); ++n)
  {
    byte crc = compute_crc(a[n]);
    cout << "data = " << hex << uppercase << a[n] << ", CRC = " << (unsigned) crc.u8 << endl;
  }
}


[update]
You may also use bitsets
C++
#include <iostream>
#include <bitset>
using namespace std;

using byte = bitset<8>;

byte compute_crc(bitset<24> data)
{
  byte crc = 0xFF;
  byte crc_new = 0x00;
  for (int i = 23; i>=0; --i)
  {
    crc_new[7] = crc[6];
    crc_new[6] = crc[5];
    crc_new[5] = crc[4];
    crc_new[4] = crc[3] ^ crc[7];
    crc_new[3] = crc[2] ^ crc[7];
    crc_new[2] = crc[1] ^ crc[7];
    crc_new[1] = crc[0];
    crc_new[0] = data[i] ^ crc[7];
    crc = crc_new;
  }
  crc ^= 0xFF;
  return crc;
}

int main()
{
  bitset<24> a[] = { 0xA0CA85, 0x3B007C };

  for (size_t n = 0; n < sizeof(a)/sizeof(a[0]); ++n)
  {
    byte crc = compute_crc(a[n]);
    cout << "data = " << hex << uppercase << a[n].to_ulong() << ", CRC = " << crc.to_ulong() << endl;
  }
}
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900