Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Wiedermal C -> Delphi (https://www.delphipraxis.net/113394-wiedermal-c-delphi.html)

Gehstock 7. Mai 2008 10:41


Wiedermal C -> Delphi
 
versuche folgenden Code zu übersetzen

Delphi-Quellcode:
var
char Test_PATCH[17],Test74[17],Test9C[17];
 char TestCC[33];
....
 {case 2:
 for(i=0;i<16;i++)
 {
  if(!(i%2)) //Prüfung auf Parität (is i/2 ???)
  {
    Test74[i]=Test_PATCH[i+1];
  }
  else
  {
  Test74[i]=Test_PATCH[i-1];
  }

hab ich nach


Delphi-Quellcode:
var
Test_PATCH : Array [1..17] of {Char}Word;//[b]hier überlege ich auch noch[/b]
Test74 : Array [1..17] of {Char}Word;
Test9C : Array [1..17] of {Char}Word;
TestCC : Array [1..33] of {Char}Word;
...
 2 : for i:=0 to 16  do
  begin
   if(not (i mod 2)) then //Prüfung auf Parität (is i/2 ???) [b]Hier hapert es Was soll "i mod 2"nicht sein[/b]
    Test74[i]:=Test_PATCH[i+1]
  else
  Test74[i]:=Test_PATCH[i-1];
 end;
Übersetzt


und wie übersetze ich
Delphi-Quellcode:
int Test_code(char* Test, char *std)
und
Delphi-Quellcode:
int main(int argc, char* argv[])

und ist diese zeile
Delphi-Quellcode:
TestCC[i*4]=(((Test74[(i*2)+1]&dest[(i*2)+1])|Test9C[(i*2)+1])&Test9C[(i*2)+1])%10;
mit
Delphi-Quellcode:
TestCC[i*4]:=(((Test74[(i*2)+1] and dest[(i*2)+1]) or Test9C[(i*2)+1]) and Test9C[(i*2)+1])mod 10;
richtig übersetzt?

Klaus01 7. Mai 2008 10:49

Re: Wiedermal C -> Delphi
 
ein Versuch:

Code:
int main(int argc, char* argv[])
Das sind die Aufrufparameter des Programms
argc entspricht ParamCount
argv entspricht Paramstr

Delphi-Quellcode:
var
  Test_PATCH : Array [1..17] of Byte
  Test74 : Array [1..17] of Byte;
  Test9C : Array [1..17] of Byte;
  TestCC : Array [1..33] of Byte;
...
2 : for i:=0 to 16  do
  begin
   if (i mod 2)= 0 then //Prüfung auf Parität (is i/2 ???) [b]Hier hapert es Was soll "i mod 2"nicht sein[/b]
    Test74[i]:=Test_PATCH[i+1]
  else
  Test74[i]:=Test_PATCH[i-1];
end;
Code:
int Test_code(char* Test, char *std)
Delphi-Quellcode:
function Test_code(Test:array of byte;std :array of byte):Integer
und zum Schluss noch ein Link zu C von A bis Z, ein OnlineBuch zu C.

Grüße
Klaus

nicodex 7. Mai 2008 14:01

Re: Wiedermal C -> Delphi
 
Delphi-Quellcode:
procedure Test();
var
  TestPatch: array [0..16] of Byte;
  Test74: array [0..16] of Byte;
  I: Integer;
begin
(** )
  case Bar of
    2:
(**)
      for I := 0 to 15 do
        if not Odd(I) then // (I mod 2) = 0
          Test74[I] := TestPatch[I + 1]
        else
          Test74[I] := TestPatch[I - 1];
(** )
    end;
  end;
(**)
end;
Letzendlich wird dort nichts anders gemacht als 8 'Words' aus TestPatch mit vertauschtem Lo-/Hi-Byte in Test74 zu kopieren.
Wobei das letzte Mitglied beider Arrays ausgelassen wird (scheint nur ein Platzhalten zu sein).
Wenn man die Datentypen ändern würde, dann könnte man es so schreiben:
Delphi-Quellcode:
procedure Test();
type
  TTest = packed record
    case Integer of
      1: (
        Bytes: array [0..16] of Byte);
      2: (
        Words: array [0..7] of Word{;
        Reserved: Byte});
  end;
var
  TestPatch: TTest;
  Test74: TTest;
  I: Integer;
begin
  for I := 0 to 7 do
    Test74.Words[I] := Swap(TestPatch.Words[I]);
end;
Was effizienten/lesbaren Code ergibt...

SirThornberry 7. Mai 2008 14:48

Re: Wiedermal C -> Delphi
 
was genau hast du vor? willst du den Quelltext wirklich 1 zu 1 nachbilden um die Funktionen zum Beispiel in einer DLL kompatible zu C zu exportieren?
Oder soll das ganze dann ausschließlich in Delphi bleiben?

Je nach dem was von beiden du vor hast würde man es unterschiedlich machen.

Beispiel:
Code:
int Test_code(char* Test, char *std)
1 zu 1 übersetzung:
Delphi-Quellcode:
function Test_code(Test: PChar; std: PChar)
Übliche Umsetzung unter Delphi
Delphi-Quellcode:
type
  TCharArray = Array of char;
[...]
function Test_code(Test: TCharArray; std: TCharArray)
Zweiteres ist nicht mehr kompatibel zu C. Du kannst also ein TCharArray nicht einfach an eine DLL-Funktion geben welche in wie oben beschrieben geschrieben wurde.

Gehstock 7. Mai 2008 22:54

Re: Wiedermal C -> Delphi
 
Bleibt ausschließlich bei delphi ist nur ein versuch einen Algorithmus zu Übersetzen

inherited 7. Mai 2008 23:33

Re: Wiedermal C -> Delphi
 
Zitat:

Zitat von Gehstock
versuche folgenden Code zu übersetzen

Delphi-Quellcode:
var
  test_PATCH, Test74, Test9C, TestCC: String; // Ruhig string benutzen char Test_PATCH[17],Test74[17],Test9C[17];
  //char TestCC[33];
....
case whatever of
 2: // {case 2:
 for i := 1 to 16 do // 1 to 16, in Delphi beginnen String-Indizes bei 1, in C bei 0 (normalerweise) for(i=0;i<16;i++)
 begin // {
 if (i mod 2 = 0) then // if(!(i%2)) //Prüfung auf Parität (is i/2 ???)
 begin // {
   Test74[i]=Test_PATCH[i+1];
 end // }
 else
 begin {
   Test74[i]=Test_PATCH[i-1];
 end;// }
Zitat:

und wie übersetze ich
Delphi-Quellcode:
int Test_code(char* Test, char *std)
und
Delphi-Quellcode:
int main(int argc, char* argv[])

->

Delphi-Quellcode:
function Test_code(Test, std: String): Integer;
das andere ist der Kopf der main-Methode, in Pascal also der Rumpf begin...end. in der main-Unit.

Zitat:

und ist diese zeile
Delphi-Quellcode:
TestCC[i*4]=(((Test74[(i*2)+1]&dest[(i*2)+1])|Test9C[(i*2)+1])&Test9C[(i*2)+1])%10;
mit
Delphi-Quellcode:
TestCC[i*4]:=(((Test74[(i*2)+1] and dest[(i*2)+1]) or Test9C[(i*2)+1]) and Test9C[(i*2)+1])mod 10;
richtig übersetzt?
Delphi-Quellcode:
TestCC[i*4+1] := (((Test74[(i*2)+2] and dest[(i*2)+2]) or Test9C[(i*2)+2]) and Test9C[(i*2)+2]) mod 10;
Es ist allerdings schwer zu sagen ob das dem oberen entspricht weil ich den Zusammenhang bzw das Ziel des Codefragments nicht kenne.


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

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