![]() |
Delphi-Version: 5
For-loop from C to Delphi
Could you help translate this:
Code:
to something that can be understable for me (Delphi). I tried (but I know is bad):
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; } }
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; |
AW: From for-loop C to Delphi
The Index starts with 0
Delphi-Quellcode:
or
for I := 0 to Length(Data)-1 do
Delphi-Quellcode:
for I := Low(data) to High(Data) do
|
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.
|
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; |
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:
In Delphi it mean 'X' or #88? What sense is this if-statement?
if(temp[i] == 'X') {
BTW: This is fragment from telepen.c from Zint project. |
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.
|
Re: For-loop from C to Delphi
Thare are these functions:
Code:
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()?
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)); } }
Code:
What values are assigned to i in these cases?
s = "C++";
i = atoi(s[0]); i = ctoi(s[0]); i = s[0];
Delphi-Quellcode:
?
s = 'C++';
i = Ord(s[0]); i = StrToInt(s[0]); i = Ord(s[0]); |
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 |
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.
|
AW: For-loop from C to Delphi
As shown in #4 ;)
|
AW: For-loop from C to Delphi
Easy to forget, since Union seemed to use a zero base on Strings, which can lead to quite some confusion - I know that first hand, since I regularly forget it myself. But you're right of course :)
|
AW: For-loop from C to Delphi
Yes this was for PChar (see StrCopy). But the principle was to show the StrToInt() and Ord() function's results.
|
AW: For-loop from C to Delphi
I tried to translate the two posted functions:
Delphi-Quellcode:
Untested, use at your own risk.
function ctoi(source: char): integer;
begin if (source >= '0') and (source <= '9') then Result := Ord(source) - Ord('0') else Result := Ord(source) - Ord('A') + 10; end; function itoc(source: integer): char; begin if (source >= 0) and (source <= 9) then Result := Chr(Ord('0') + source) else Result := Chr(Ord('A') + source - 10); end; |
AW: For-loop from C to Delphi
Oder kürzer Or shorter:
Delphi-Quellcode:
function ctoi(source: char): integer;
begin result := StrToInt('$'+source); end; function itoc(source: integer): char; begin c := Format('%x', [source])[1]; end; |
AW: For-loop from C to Delphi
And what about ctoi('Z')?
|
AW: For-loop from C to Delphi
Ok, my solution works only for hex :(
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:46 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-2025 by Thomas Breitkreuz