AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

For-loop from C to Delphi

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

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

For-loop from C to Delphi

  Alt 18. Sep 2011, 17: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.869 Beiträge
 
Delphi 11 Alexandria
 
#2

AW: From for-loop C to Delphi

  Alt 18. Sep 2011, 17: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.492 Beiträge
 
Delphi 7 Enterprise
 
#3

AW: For-loop from C to Delphi

  Alt 18. Sep 2011, 17: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.655 Beiträge
 
Delphi 12 Athens
 
#4

AW: For-loop from C to Delphi

  Alt 18. Sep 2011, 18: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
482 Beiträge
 
Delphi XE6 Professional
 
#5

Re: For-loop from C to Delphi

  Alt 18. Sep 2011, 18: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 18:56 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.655 Beiträge
 
Delphi 12 Athens
 
#6

AW: For-loop from C to Delphi

  Alt 19. Sep 2011, 08: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
Antwort Antwort

Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

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 00:33 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