AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

For-loop from C to Delphi

Ein Thema von WojTec · begonnen am 18. Sep 2011 · letzter Beitrag vom 19. Sep 2011
Antwort Antwort
Seite 1 von 2  1 2      
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

For-loop from C to Delphi

  Alt 18. Sep 2011, 16:13
Delphi-Version: 5
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;
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.851 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: From for-loop C to Delphi

  Alt 18. Sep 2011, 16:15
The Index starts with 0
for I := 0 to Length(Data)-1 do or
for I := Low(data) to High(Data) do
Markus Kinzler
  Mit Zitat antworten Zitat
Benutzerbild von Union
Union

Registriert seit: 18. Mär 2004
Ort: Luxembourg
3.487 Beiträge
 
Delphi 7 Enterprise
 
#3

AW: For-loop from C to Delphi

  Alt 18. Sep 2011, 16:29
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.
Ibi fas ubi proxima merces
sudo /Developer/Library/uninstall-devtools --mode=all
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: For-loop from C to Delphi

  Alt 18. Sep 2011, 17:25
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] = 'Xthen
      raise Exception.Create('Invalid position of X in Telepen data');
    if Data[i + 1] = 'Xthen
      Count := Count + Ord(Data[i]) + 17
    else
      Count := Count + 27 + (Ord(Data[i]) * 10) + Ord(Data[i + 1]);
    inc(i, 2);
  end;
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#5

Re: For-loop from C to Delphi

  Alt 18. Sep 2011, 17:53
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.

Geändert von WojTec (18. Sep 2011 um 17:56 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: For-loop from C to Delphi

  Alt 19. Sep 2011, 07:16
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.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#7

Re: For-loop from C to Delphi

  Alt 19. Sep 2011, 11:41
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]);
?
  Mit Zitat antworten Zitat
Benutzerbild von Union
Union

Registriert seit: 18. Mär 2004
Ort: Luxembourg
3.487 Beiträge
 
Delphi 7 Enterprise
 
#8

AW: For-loop from C to Delphi

  Alt 19. Sep 2011, 11:52
Delphi-Quellcode:
StrCopy(s, 'C++');
i := Ord(s[0]); -> 67
i := StrToInt(s[0]); -> Invalid since 'Cis no number
Ibi fas ubi proxima merces
sudo /Developer/Library/uninstall-devtools --mode=all
  Mit Zitat antworten Zitat
Medium

Registriert seit: 23. Jan 2008
3.679 Beiträge
 
Delphi 2007 Enterprise
 
#9

AW: For-loop from C to Delphi

  Alt 19. Sep 2011, 12:02
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.
"When one person suffers from a delusion, it is called insanity. When a million people suffer from a delusion, it is called religion." (Richard Dawkins)
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.542 Beiträge
 
Delphi 11 Alexandria
 
#10

AW: For-loop from C to Delphi

  Alt 19. Sep 2011, 12:03
As shown in #4
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:33 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