Einzelnen Beitrag anzeigen

philipp.hofmann

Registriert seit: 21. Mär 2012
Ort: Hannover
864 Beiträge
 
Delphi 10.4 Sydney
 
#1

Checksum-Berechnung nach vorgegebenen C-Code

  Alt 15. Jun 2023, 14:39
Wie übersetzt man den folgenden Code von C nach Delphi (bzw. Pascal):

Delphi-Quellcode:
FIT_UINT16 FitCRC_Get16(FIT_UINT16 crc, FIT_UINT8 byte)
{
  static const FIT_UINT16 crc_table[16] =
  {
      0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401,
      0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400
   }
;
   FIT_UINT16 tmp;

   // compute checksum of lower four bits of byte
   tmp = crc_table[crc & 0xF];
   crc = (crc >> 4) & 0x0FFF;
   crc = crc ^ tmp ^ crc_table[byte & 0xF];

   // now compute checksum of upper four bits of byte
   tmp = crc_table[crc & 0xF];
   crc = (crc >> 4) & 0x0FFF;
   crc = crc ^ tmp ^ crc_table[(byte >> 4) & 0xF];

   return crc;
}
Ich habe mal angefangen, aber mindestens das "byte" muss noch adäquat ersetzt werden:

Delphi-Quellcode:
CRC_TABLE: TArray<word> = [$000, $CC01, $D801, $1400, $F001, $3C00, $2800, $E401, $A001, $6C00, $7800, $B401, $5000, $9C01, $8801, $4400];

procedure TFITWriter.ByteCRC(Data:Byte);
begin
   var tmp:Word;

   // compute checksum of lower four bits of byte
   tmp:=CRC_TABLE[crc and $F];
   crc:=(crc shr 4) and $0FFF;
   crc:=crc xor tmp xor CRC_TABLE[byte and $F];

   // now compute checksum of upper four bits of byte
   tmp:=CRC_TABLE[crc and $F];
   crc:=(crc shr 4) and $0FFF;
   crc:=crc xor tmp xor CRC_TABLE[(byte shr 4) and $F];
end;
Ich führe crc im Gegensatz zum C-Code oben als word-Variable mit. Daher ist das return nicht notwendig.

Falls hier jemand helfen kann, wäre ich dankbar.
  Mit Zitat antworten Zitat