Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi For-loop from C to Delphi (https://www.delphipraxis.net/163176-loop-c-delphi.html)

WojTec 18. Sep 2011 16:13

Delphi-Version: 5

For-loop from C to Delphi
 
Could you help translate this:

Code:
   for (i = 0; i < temp_length; i += 2)
   {
      if(temp[i] == 'X') {
         strcpy(symbol->errtxt, "Invalid position of X in Telepen data");
         return ZERROR_INVALID_DATA;
      }
      
      if(temp[i + 1] == 'X') {
         glyph = ctoi(temp[i]) + 17;
         count += glyph;
      } else {
         glyph = (10 * ctoi(temp[i])) + ctoi(temp[i + 1]);
         glyph += 27;
         count += glyph;
      }
   }
to something that can be understable for me (Delphi). I tried (but I know is bad):

Delphi-Quellcode:
  for I := 1 to Length(Data) do
  begin
    if Ord(Data[I + 1]) = 88 then
      Count := Count + StrToInt(Data[I]) + 17
    else
      Count := Count + (10 * StrToInt(Data[i])) + StrToInt(Data[i + 1]) + 17
    ;
  end;
:?:

mkinzler 18. Sep 2011 16:15

AW: From for-loop C to Delphi
 
The Index starts with 0
Delphi-Quellcode:
for I := 0 to Length(Data)-1 do
or
Delphi-Quellcode:
for I := Low(data) to High(Data) do

Union 18. Sep 2011 16:29

AW: For-loop from C to Delphi
 
This is not directly possible using a Delphi for, since OP doesn't support increments or decrements other than 1. Either use while / repeat construct or calculate the for range as well as all values derived from the counter.

DeddyH 18. Sep 2011 17:25

AW: For-loop from C to Delphi
 
Since we cannot know the type of Data, here are 2 suggestions:
Delphi-Quellcode:
//If Data is a string:
i := 1;
while i < Length(Data) do

//If Data is a dynamic array of Char:
i := 0;
while i < High(Data) do

//Anyway, I hope this is right in both cases:
  begin
    if Data[i] = 'X' then
      raise Exception.Create('Invalid position of X in Telepen data');
    if Data[i + 1] = 'X' then
      Count := Count + Ord(Data[i]) + 17
    else
      Count := Count + 27 + (Ord(Data[i]) * 10) + Ord(Data[i + 1]);
    inc(i, 2);
  end;

WojTec 18. Sep 2011 17:53

Re: For-loop from C to Delphi
 
Oh, I forgot: this code working on string.

I'm not sure valid is Ord() or StrToInt().
In C++ "char" is Byte in Delphi, but value contain ASCII or what?

Also I dont understand this statement:

Code:
if(temp[i] == 'X') {
In Delphi it mean 'X' or #88? What sense is this if-statement?

BTW: This is fragment from telepen.c from Zint project.

DeddyH 19. Sep 2011 07:16

AW: For-loop from C to Delphi
 
I am not very familiar with C, but I think ctoi means "character to integer", which is Ord() in Delphi. There is also a function called atoi which means "ASCII to integer" (Delphi: StringToInt). Both return different results. In my opinion Ord() is the right function for your purpose.

WojTec 19. Sep 2011 11:41

Re: For-loop from C to Delphi
 
Thare are these functions:

Code:
int ctoi(char source)
{ /* Converts a character 0-9 to its equivalent integer value */
   if((source >= '0') && (source <= '9'))
      return (source - '0');
   return(source - 'A' + 10);
}

char itoc(int source)
{ /* Converts an integer value to its hexadecimal character */
   if ((source >= 0) && (source <= 9)) {
      return ('0' + source); }
   else {
      return ('A' + (source - 10)); }
}
StrToInt working. But I'm still confused, what difference between ctoi() and atoi() if char in C is byte? You told C.atoi() = Delphi.StrToInt(), the same doing ctoi()?

Code:
s = "C++";
i = atoi(s[0]);
i = ctoi(s[0]);
i = s[0];
What values are assigned to i in these cases?

Delphi-Quellcode:
s = 'C++';
i = Ord(s[0]);
i = StrToInt(s[0]);
i = Ord(s[0]);
?

Union 19. Sep 2011 11:52

AW: For-loop from C to Delphi
 
Delphi-Quellcode:
StrCopy(s, 'C++');
i := Ord(s[0]); -> 67
i := StrToInt(s[0]); -> Invalid since 'C' is no number

Medium 19. Sep 2011 12:02

AW: For-loop from C to Delphi
 
More importantly: When working with Delphi Strings, keep in mind that their index starts with 1! If you have an array of char, the first index is zero though.

DeddyH 19. Sep 2011 12:03

AW: For-loop from C to Delphi
 
As shown in #4 ;)


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:07 Uhr.
Seite 1 von 2  1 2      

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