CRC calculation

Fluxanode

Well-known member
I have a binary String of "011001000010100100100010", 24 bits, how do I calculate CRC8? I tried using FastCRC but testing gives me the wrong results. I think it's because it is looking for bytes?

Thanks
 
I am using the CRC8 example for FastCRC and comparing it to an online calculator that calculates the crc from a binary set of numbers.
Poly = x8 + x2 + x1 + 1

I don't get the same results.

Code:
 /*
  FastCRC-Example
  (c) Frank Boesing 2014
*/

#include <FastCRC.h>

FastCRC8 CRC8;

uint8_t buf[24] = {'1', '0', '0', '0', '0', '0', '0', '0' , '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};

void setup() {

  delay(1500);
  Serial.begin(115200);

  Serial.println("CRC Example");
  Serial.println();

  Serial.print("SMBUS-CRC of \"");

  for (unsigned int i = 0; i < sizeof(buf); i++) {
    Serial.print((char) buf[i]);
  }

  Serial.print("\" is: 0x");
  Serial.println( CRC8.smbus(buf, sizeof(buf)), HEX );

}


void loop() {
}
 
Last edited:
You appear to be generating a CRC on 24 bytes, or 192 bits; not on 3 bytes or 24 bits.

Could the data just be int8[] type. Why is it in a string?

If it needs to be in a string, you could write a function to convert it to an int8 array. Perhaps a library function like strtol may help convert it, although I am not sure if that function is available in Arduino.
 
You have to encode those 24 bits as 3 bytes - in hex they are 0x64,0x29,0x22. This website gives 0x93 as the CRC8 of those three bytes.
FastCRC agrees:
Code:
 /*
  FastCRC-Example
  (c) Frank Boesing 2014
*/

#include <FastCRC.h>

FastCRC8 CRC8;

//uint8_t buf[24] = {'1', '0', '0', '0', '0', '0', '0', '0' , '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'};
uint8_t buf[3] = {0x64,0x29,0x22};
void setup()
{
  Serial.begin(115200);
  while(!Serial);
  
  Serial.println("CRC Example");
  Serial.println();

  Serial.print("SMBUS-CRC of \"");

  for (unsigned int i = 0; i < sizeof(buf); i++) {
    Serial.printf("0x%02x,",buf[i]);
  }
  
  Serial.print("\" is: 0x");
  Serial.println( CRC8.smbus(buf, sizeof(buf)), HEX );
}


void loop()
{
}

Code:
CRC Example

SMBUS-CRC of "0x64,0x29,0x22," is: 0x93

Pete
 
FastCRC and the website also agree when given the input as a character string.
Code:
 /*
  FastCRC-Example
  (c) Frank Boesing 2014
*/

#include <FastCRC.h>

FastCRC8 CRC8;

const char *buf = "011001000010100100100010";
void setup()
{
  Serial.begin(115200);
  while(!Serial);
  
  Serial.println("CRC Example");
  Serial.println();

  Serial.print("SMBUS-CRC of \"");
  Serial.print(buf);
  Serial.print("\" is: 0x");
  Serial.println( CRC8.smbus(buf, strlen(buf)), HEX );
}


void loop()
{
}

Code:
CRC Example

SMBUS-CRC of "011001000010100100100010" is: 0x7D

Pete
 
Back
Top