![]() |
Java to pascal/delphi
:arrow: Hi, can anyone help me with this code written i java..
If you have the required knowledge, please help.! I started translating this JAVA into pascal code:
Delphi-Quellcode:
>>>>>>>>>> And now my (poor) result in rewriting it, many og the for loops and stuff I DIDNT manedge to translate, but here goes:
public static String decodeString(String s)
{ return decodeString(new QStringBuffer(), s).toString(); } public static QStringBuffer decodeString(QStringBuffer qstringbuffer, String s) { if(s == null) return qstringbuffer; int i = s.length(); for(int j = 0; j < i; j++) { char c = s.charAt(j); if(c == '&') { if(i < j + 5) break; int k = 0; for(int l = 0; l < 4; l++) { j++; k *= 16; k += s.charAt(j) - 65; } qstringbuffer.append((char)k); } else { qstringbuffer.append(c); } } return qstringbuffer; }
Delphi-Quellcode:
>>> So How am I dooing so far?.. Any corrections, comments or hints?
function decodeString(String s)
begin result:= decodeString(new QStringBuffer(), s).toString(); end; function QStringBuffer decodeString(QStringBuffer qstringbuffer, String s) var i,k,j,l:integer; c:char; qstringbuffer: Tstringlist; begin if(s == null) result:= qstringbuffer; i = length(s); for(j = 0; j < i; j++) begin c = s[j]; if c ='&' then begin if(i < j + 5) break; k = 0; for(l = 0; l < 4; l++) begin j:=j+1; k :=k*16; k =k+s[j] - 65; end; qstringbuffer.append((char)k); end else begin qstringbuffer.append(c); end; end; result:= qstringbuffer; end; Thanks in advance! [b]Regars PasTra |
Re: HELP: JAVA TO PASCAL/DELPHI
First, try to use the BBCode of the Board. Use the [delphi]-Tag oder the [code]-Tag!
|
Re: HELP: JAVA TO PASCAL/DELPHI
Zitat:
Sorry I did not realize that. I have changed it now.. Any advices ideas to the snippers? |
Re: HELP: JAVA TO PASCAL/DELPHI
When you wait about ten minutes then i have a solution.
What makes exactly this code? Because i doesn't know how to program with Java ;)
Code:
return decodeString(new QStringBuffer(), s).toString();
|
Re: HELP: JAVA TO PASCAL/DELPHI
Zitat:
The way I see it Decodestring is called like like this: var s:string begin s:= DecodeString('Hello'); end; |
Re: HELP: JAVA TO PASCAL/DELPHI
Zitat:
|
Re: HELP: JAVA TO PASCAL/DELPHI
LOL 'what makes this code' i think he 'makes' nothing. he is doing a lot ;)
My Java isn't good enough to translate, but here some tips for pascal-language: to compare to variables, you use '=', e.g.
Delphi-Quellcode:
to assign a variable you use ':=', e.g
if s1=s2 then whatever;
Delphi-Quellcode:
Furthermore there is no
s1:=s2;
Code:
in delphi/pascal, to increase the value of an integer-var by 1, use
l++
Delphi-Quellcode:
or
i:=i+1;
Delphi-Quellcode:
so far
inc(i);
inh3r1ted |
Re: HELP: JAVA TO PASCAL/DELPHI
Try this code - there is no guaranty that it's right :thumb:
Delphi-Quellcode:
// Edit
function decodeStringF2(QStringBuffer : TStringList; s: String): TStringList;
var i, k, j, l : Integer; c : Char; Buffer : TStringList; begin Buffer := QStringBuffer; if(s <> '') then begin i := length(s); // i had to replace the for because i can't change an counter in a for j := 1; // Index in a String is different - in Delphi it starts with 1 while j < i do begin c := s[j]; if c = '&' then begin if (i < j+5) then break; k := 0; for l := 0 to 3 do begin inc(j); k := k*16; k := k + StrToInt(s[j]) - 65; // Here you habe to be careful! The character here must be a number! end; end else begin Buffer.Append(c); end; inc(j); end; end; Result := Buffer; end; function decodeStringF1(s: String): String; var Buffer : TStringList; begin Buffer := TStringList.Create; Buffer := decodeStringF2(Buffer, s); Result := Buffer.Text; end; Deleted some test-code! |
Re: HELP: JAVA TO PASCAL/DELPHI
Hi!
... and Welcome in DP! I can't help you with your problem but I have a little request to you: Could you please change the title of this topic? (simply edit your first post) There a lot of people who are searching for help and so it's not necessary to say "help"! Furthermore the title is easier to read if it's not UPPERCASE! Thank you! Ciao Frederic |
Re: HELP: JAVA TO PASCAL/DELPHI
Zitat:
I have changed it now.. |
Re: HELP: JAVA TO PASCAL/DELPHI
Hi s.h.a.r.k!
Thanks for translating the code, I am about to test it now.... You wrote: j := 1; // Index in a String is different - in Delphi it starts with 1 i fixed it like this: while j <= i do otherwise the last char didn't get processed.. I will return later and tell you how it is going.. For now BIG thanks and by the way, I really think this forum "ROX" Chears Pastra |
Re: Java to pascal/delphi
Zitat:
|
Re: Java to pascal/delphi
Hi,
I think there are some little mistakes you should fix. First of all, you don't need any kind of StringBuffer in Delphi. To Java a String is just a static class and each assignment will create a new instance (and the GC have to free the old unused one). Therefore a buffer (no fixed size) encapsulates the functianality of working with strings that are supposed to change there size. Although this is next to the Delphi-behaviour working with Strings you know how many characters are used in your string and you can allocate this memory before returning the string (of cause you can use the TStringList, but no need to do so!) Lets have a look at this part
Delphi-Quellcode:
Well there is something missing, don't you think? Although k is changed, no one will ever know. And Java does automaticaly cast a single Character into a short value, that does not mean, that the string will be casted into the represented value (StrToInt) but the byte value of this string (e.g. A = 65).
if c = '&' then
begin if (i < j+5) then begin break; end; k := 0; for l := 0 to 3 do begin inc(j); k := k*16; k := k + StrToInt(s[j]) - 65; // Here you habe to be careful! The character here must be a number! end; end Just change the sourcelines to
Delphi-Quellcode:
Regards Der Unwissende
if c = '&' then
begin if (i < j+5) then begin break; end; k := 0; for l := 0 to 3 do begin inc(j); k := k*16; k := k + ord(s[j]) - 65; // Here you habe to be careful! The character here must be a number! end; buffer.append(chr(k)); end |
Re: Java to pascal/delphi
Hi again, thanks it works now. Yes I see what you meen by + ord(s[j])..
Thanks! |
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:08 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