Use Cardinal instead of Integer - because the C code uses "unsigned int".
Your for should be 0-based:
for currByte := 0 to dataLength-1 do
^ maps to xor
if ((dataStore xor accum) and $8000) > 0 then
accum := (accum shl 1) xor CRCDIV;
Lastly, this does not affect your code, but for correctness:
for currBit := 0 to 7 do
Interesting polynomial:
CRCDIV = $8005;
----- Original Message -----
From: James Sugrue
To: Multiple recipients of list delphi
Sent: Tuesday, November 04, 2003 11:50 AM
Subject: [DUG]: C Conversion
I have the following code. I have tried to convert into Delphi but it isn't
100% correct. Could some kind soul show me where I've gone wrong?
#define CRCDIV 0x8005
unsigned int computeCRC (unsigned char *data, unsigned int dataLength)
{
unsigned int accum;
unsigned int currByte;
unsigned int currBit;
unsigned int dataStore;
accum = 0;
// repeat for all bytes in message from the header to the data
for (currByte = 0; currByte < dataLength; currByte++) {
dataStore = ((unsigned int) data[currByte]) << 8;
for (currBit = 0; currBit < 8; currBit++) {
if ((dataStore ^ accum) & 0x8000) {
accum = (accum << 1) ^ CRCDIV;
}
else {
accum <<= 1;
}
dataStore <<= 1;
}
}
return (accum);
}
function computeCRC (data : PChar; dataLength : Integer) : Integer;
const
CRCDIV = $8005;
var
accum : Integer;
currByte : Integer;
currBit : Integer;
dataStore : Integer;
begin
accum := 0;
// repeat for all bytes in message from the header to the data
for currByte := 1 to dataLength do
begin
dataStore := Integer(data[currByte]) shl 8;
for currBit := 1 to 8 do
begin
if ((dataStore or accum) and $8000) > 0 then
begin
accum := (accum shl 1) or CRCDIV;
end
else
begin
accum := accum shl 1;
end;
dataStore := dataStore shl 1;
end;
end;
result := accum;
end;
James Sugrue
Software Developer
WA Systems
Timaru
Phone 03 688-1131
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/