Solved

# CRC16 Calculation Algorithm (good, but...)

Posted on 2004-11-02
19,193 Views
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
Question by:Obsolate

LVL 1

Expert Comment

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 Comment

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

LVL 1

Expert Comment

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

LVL 1

Accepted Solution

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

### Suggested Solutions

DirectXFramework 5 93
Sublime text 2 IDE - yeoman 4 145
VB.Net Reusable Form Layout 4 114
missing "main" in Java on Eclipse 4 55
In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.