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

# 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.

thanx
0
Obsolate
• 3
1 Solution

Commented:
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

Author 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

Commented:
>> is a bitwise shift right operator shr is the pascal/delphi equivelent.

so >> should be replaced by shr
<< (if there were any) should be replaced by shl for a left shift
0

Commented:
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

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