• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 19582
  • Last Modified:

CRC16 Calculation Algorithm (good, but...)

Polynomial: x16 + x12 + x5 + 1
Start Value: 0xFFFF

CRC_POLYNOM = 0x8408;
CRC_PRESET = 0xFFFF;

C-Example:

unsigned internal CRC = CRC_PRESET;
for (i = 0; i < cnt; i++) /* cnt = number of protocol bytes without CRC */
{
crc ^= DATA[i];
for (j = 0; j < 8; j++)
{
if (crc & 0x0001)
crc = (crc >> 1) ^ CRC_POLYNOM;
else
crc = (crc >> 1);
}
}

i want to this code translate DELPHI.

please.

thanx
0
Obsolate
Asked:
Obsolate
  • 3
1 Solution
 
c567591Commented:
Here is a rough translation:
Translated by OpenCtoPas (http://c2pas.sourceforge.net/)

unsigned internal CRC := CRC_PRESET;
for (i := 0; i < cnt; i++) (* cnt = number of protocol bytes without CRC *)
begin
crc ^ := DATA[i];
for (j := 0; j < 8; j++)
begin
if (crc or 0x0001) then
crc := (crc >> 1) ^ CRC_POLYNOM
else
crc := (crc >> 1);
end;
end;
0
 
ObsolateAuthor Commented:
After Translate (C2PAS) :

for (i := 0; i < cnt; i++) (* cnt = number of protocol bytes without CRC *)
begin
crc ^ := DATA[i];
for (j := 0; j < 8; j++)
begin
if (crc (* C2PAS: RefOrBit? *)& 0x0001) then
crc := (crc >> 1) ^ CRC_POLYNOM
else
crc := (crc >> 1);
end;
end;

what is this?? ( >> )
please (400 point :) ) i want to this code translatee..
0
 
c567591Commented:
>> is a bitwise shift right operator shr is the pascal/delphi equivelent.
Sorry about that. :)

so >> should be replaced by shr
<< (if there were any) should be replaced by shl for a left shift
0
 
c567591Commented:
Here, this compiled for me.
There may be minor things that remain but this should get you there.

procedure CRC16 (cnt: integer; DATA: ShortString);
var
i,j: Integer;
crc: Integer;
CRC_POLYNOM: Integer;
begin
  CRC_POLYNOM := $1021;
  for i := 0 to cnt do (* cnt = number of protocol bytes without CRC *)
    begin
       crc := crc xor integer (DATA[i]);
       for j := 0 to 8 do
         begin
           if ((crc or $0001)<>0) then
             crc := (crc shr 1) xor CRC_POLYNOM
           else
             crc := (crc shr 1);
         end;
     end;
end;
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now