Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   csum-Function von C++ nach Delphi (https://www.delphipraxis.net/76373-csum-function-von-c-nach-delphi.html)

Sebastian R. 3. Sep 2006 09:28


csum-Function von C++ nach Delphi
 
Hi Leute,
ich zerbrech mir schon seit geraumer Zeit den Kopf darüber, wie ich folgenden C++-Code in Delphi übersetzen kann.

Code:
 int csum(const void *bufv, int length)
 {
    const unsigned short *buf = (const unsigned short *)bufv;
    unsigned long result = 0;

    while (length > 1) {
        result += *(buf++);
        length -= sizeof(*buf);
    }
    if (length) result += *(unsigned char*)buf;
    result = (result >> 16) + (result & 0xFFFF);
    result += (result >> 16);
    result = (~result)&0xFFFF;

    return (int)result;
 }
Könnt ihr mir helfen?

Danke im Voraus, Mfg, Sebastian R.!

JasonDX 3. Sep 2006 09:46

Re: csum-Function von C++ nach Delphi
 
Hi

Zitat:

Zitat von Sebastian R.
Code:
 int csum(const void *bufv, int length)
 {
    const unsigned short *buf = (const unsigned short *)bufv;
    unsigned long result = 0;

    while (length > 1) {
        result += *(buf++);
        length -= sizeof(*buf);
    }
    if (length) result += *(unsigned char*)buf;
    result = (result >> 16) + (result & 0xFFFF);
    result += (result >> 16);
    result = (~result)&0xFFFF;

    return (int)result;
 }

Kompiliert, probiert hab ichs aber nicht. Sollte jedoch so ziemlich das selbe sein:
Delphi-Quellcode:
function csum(const bufv: Pointer; length: integer): integer;
var
  res, bufIndex: cardinal;
  buf: packed array of word;
begin
  res := 0;
  bufIndex := 0;
  buf := bufv;
  while (length > 1) do
  begin
    inc(res, buf[bufIndex]);
    inc(bufIndex);
    dec(length, sizeof(word));
  end;
  res := (res shr 16) + (res and $FFFF);
  inc(res, res shr 16);
  res := (not res) and $FFFF;
  result := Integer(res);
end;
Der einzige groessere unterschied zur Implementierung oben ist, dass ich nicht buf, also den ArrayZeiger inkrementiere, sondern dass ich ueber einen Index (bufIndex) nacheinander auf die Elemente zugreife. ;)

greetz
Mike


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:32 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz