Einzelnen Beitrag anzeigen

Muetze1
(Gast)

n/a Beiträge
 
#3

Re: Spezielles CRC-16: Übersetzung aus C++ (omg)

  Alt 13. Apr 2008, 16:39
Einfach hier im Beitragseditor getippt, also wohl noch ein paar syntaktische Fehler drin. Ansonsten stur übersetzt, samt der manchmal unnötigen Typecasts.
Delphi-Quellcode:
function crc16(const AData: Char; APoly, AResult: word): word;
// Appends one byte to a runing CRC-16 calculation
var
  i: integer;
begin
  AResult := AResult xor word(word(AData) shl 8));

  for i := 1 to 8 do
  begin
    if ( AResult and $8000 ) <> 0 then
      AResult := (AResult shl 1) xor APoly
    else
      AResult := AResult shl 1;
  end;

  result := AResult;
end;

function file_crc16(const AFilename: string): word;
// Calculates the CRC-16 of the ISF file
var
  lFile: TStream; // finp
  lData: Char; // crcd
  lCRC: Word; // crcr
begin
  lCRC := $ffff;
  
  try
    lFile := TFileStream.Create(AFilename, fmOpenRead);
    try
      lData := '0';

      while ( lFile.Read(lData, 1) = 1 ) and ( lData >= ' ' ) do ;
      while ( lFile.Read(lData, 1) = 1 ) and ( lData >= ' ' ) do ;

      while ( lFile.Read(lData, 1) = 1 ) do
        lCRC := crc16(lData, $1021, lCRC);
    finally
      lFile.Free;
    end;
  except
    ; // böse..., aber original hat auch keine Exceptions geworfen...
  end;

  result := lCRC;
end:
/EDIT: Fehler korrigiert
  Mit Zitat antworten Zitat