Convert C++ function to Pascal

I have basic C++ skills but need to convert this small CRC function to FPC Pascal. The C++ is way beyond my current comfort level so would appreciate some help.
I found the function here
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
16
17
//CRC-8 - based on the CRC8 formulas by Dallas/Maxim
//code released under the therms of the GNU GPL 3.0 license
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;
}



Last edited on
Discussion of Pascal is outside the scope of this forum but here are some comments to help you understand this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//CRC-8 - based on the CRC8 formulas by Dallas/Maxim
//code released under the therms of the GNU GPL 3.0 license
byte CRC8(const byte *data, byte len) {
  byte crc = 0x00; // declare variable 'crc' of type byte and initialise it to 0
  while (len--) { // execute loop 'len' times
    byte extract = *data++; // load next byte from data array into 'extract' variable
    for (byte tempI = 8; tempI; tempI--) { // inner loop through bits in the byte
      byte sum = (crc ^ extract) & 0x01; // declare 'sum' and set it to the value of 
// the least significant bit of the result of exclusive-or of 'crc' with 'extract'
      crc >>= 1; // bit-shift crc down by 1 bit
      if (sum) { // if sum is true (1) then do the next line
        crc ^= 0x8C; // exclusive-or crc with constant hexadecimal 8C and write it back into 'crc'
      }
      extract >>= 1; // bit-shift extract down by 1 bit
    }
  }
  return crc;
}

Note that byte is a user-defined type so you can't be certain that it holds signed 8-bit values without referencing the rest of the source files or compilation environment but that would be the usual way to define it.
Also if byte is a signed type that means that the two bit-shifts are arithmetic shifts - that is they replicate the sign bit
Last edited on
Topic archived. No new replies allowed.