James, I see you have converted unsigned integers to "Integer", which is a signed type.  Shouldn't they be converted to Longword or Word types, which are unsigned, depending on how C implements "unsigned int"?
 
Just my 2 cents worth
Pedrocelli
 
----- Original Message -----
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

 

Reply via email to