Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Java to pascal/delphi (https://www.delphipraxis.net/72016-java-pascal-delphi.html)

pastra 23. Jun 2006 21:11


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:
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;
    }
>>>>>>>>>> And now my (poor) result in rewriting it, many og the for loops and stuff I DIDNT manedge to translate, but here goes:

Delphi-Quellcode:
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;
>>> So How am I dooing so far?.. Any corrections, comments or hints?


Thanks in advance!

[b]Regars PasTra

s.h.a.r.k 23. Jun 2006 21:37

Re: HELP: JAVA TO PASCAL/DELPHI
 
First, try to use the BBCode of the Board. Use the [delphi]-Tag oder the [code]-Tag!

pastra 23. Jun 2006 21:41

Re: HELP: JAVA TO PASCAL/DELPHI
 
Zitat:

Zitat von s.h.a.r.k
First, try to use the BBCode of the Board. Use the [delphi]-Tag oder the [code]-Tag!


Sorry I did not realize that.


I have changed it now..


Any advices ideas to the snippers?

s.h.a.r.k 23. Jun 2006 22:14

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();

pastra 23. Jun 2006 22:22

Re: HELP: JAVA TO PASCAL/DELPHI
 
Zitat:

Zitat von s.h.a.r.k
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();

Hi again, DecodeString is function that returns a string output from a given input after "altering it", I am NOT programming i JAVA so that is my problem aswell...

The way I see it Decodestring is called like like this:

var
s:string
begin
s:= DecodeString('Hello');
end;

mkinzler 23. Jun 2006 22:25

Re: HELP: JAVA TO PASCAL/DELPHI
 
Zitat:

What makes exactly this code? Because i doesn't know how to program with Java
It creates a new QStringBuffer and Casts it to a string.

inherited 23. Jun 2006 22:26

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:
  if s1=s2 then whatever;
to assign a variable you use ':=', e.g

Delphi-Quellcode:
  s1:=s2;
Furthermore there is no
Code:
  l++
in delphi/pascal, to increase the value of an integer-var by 1, use
Delphi-Quellcode:
  i:=i+1;
or
Delphi-Quellcode:
  inc(i);
so far
inh3r1ted

s.h.a.r.k 23. Jun 2006 22:45

Re: HELP: JAVA TO PASCAL/DELPHI
 
Try this code - there is no guaranty that it's right :thumb:
Delphi-Quellcode:
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;
// Edit
Deleted some test-code!

fkerber 23. Jun 2006 23:20

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

pastra 24. Jun 2006 09:47

Re: HELP: JAVA TO PASCAL/DELPHI
 
Zitat:

Zitat von fkerber
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



I have changed it now..

pastra 24. Jun 2006 09:50

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

s.h.a.r.k 24. Jun 2006 10:42

Re: Java to pascal/delphi
 
Zitat:

i fixed it like this: while j <= i do otherwise the last char didn't get processed..
Oh, sorry, but the it has to work fine! ;)

Der_Unwissende 24. Jun 2006 11:02

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:
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
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).
Just change the sourcelines to

Delphi-Quellcode:
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
Regards Der Unwissende

pastra 25. Jun 2006 13:25

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