I need 8-Bit CRC in Pascal and C++

I am making my home automation thingy and the serial gateway is Arduino C++ on one end and Lazarus program the other.

I found this for Arduino,
http://www.leonardomiliani.com/en/2013/un-semplice-crc8-per-arduino/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
byte CRC8(const byte *data, byte len) {
  byte crc = 0x00;
  while (len--) {
    byte extract = *data++;
    for (byte tempI = 8; tempI; tempI--) {
      byte sum = (crc ^ extract) & 0x01;
      crc >>= 1;
      if (sum) {
        crc ^= 0x8C;
      }
      extract >>= 1;
    }
  }
  return crc;
}


It works fine on the Arduino, but it is well beyond my currently very basic skills with C++ to convert.

Could someone please convert this for me -- or-- point me to any libraries or snippets with matching CRC-algorithms that are written for both languages.

I found some 16-bit CRC versions in both languages but I am pressed for data length by the Arduino so one Byte would be better than than two Bytes. Also the 16-Bit versions were terribly slow on the Arduino. No surprises of course at 16MHz and the CRC8 above was acceptably fast enough.

Thanks in advance.
Do you really need a crc calculation? A crc calc typically includes a large (for the arduino) table.
You would use a crc for a long record (100 bytes)

A good hash algorithm can generate a hash code. With same cpu overhead as calculating a crc, All you need is..
 
typedef unsigned short HASH_t;    // 32bit 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HASH_t   DoHash(const char * cp)
{
     HASH_t  hash = 0 ;

    while(*cp != '\0')
    {
        hash += *cp++;
        hash += (hash<<10);
        hash ^= (hash>>6) ;
    }
    hash += (hash <<3);
    hash ^= (hash >>11);
    hash += (hash <<15);

    return hash ;
}

Save the hash as you would a crc.
Topic archived. No new replies allowed.