9 Replies - 12397 Views - Last Post: 26 August 2011 - 05:38 PM Rate Topic: -----

#1 Cantero   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-August 11

Converting functions from Delphi to C#

Posted 26 August 2011 - 11:35 AM

I have tried converting these three function to C#, but failed miserably. I'm not really asking for anyone to do it for me (Though that would be nice, I wouldn't learn anything.) just to help me out a bit on it.

function RNDString(Chars: string; Count: Integer): string;
var
  i, x: integer; 
begin 
  Result := ''; 
  for i := 0 to Count - 1 do 
  begin 
    x := Length(Chars) - Random(Length(Chars));
    Result := Result + chars[x];
    Chars := Copy(Chars, 1,x - 1) + Copy(Chars, x + 1,Length(Chars));
  end; 
end; 

function Encode(Data, SecString, CharSet: string; MinV: Integer = 0; MaxV: Integer = 5): string;
var 
  i, x: integer; 
  s1, s2, ss: string;
begin
  if minV > MaxV then
  begin 
    i := minv; 
    minv := maxv; 
    maxv := i; 
  end; 
  if MinV < 0 then MinV := 0;
  if MaxV > 100 then MaxV := 100;
  Result := ''; 
  if Length(SecString) < 16 then Exit;
  for i := 1 to Length(SecString) do
  begin
    s1 := Copy(SecString, i + 1,Length(SecString));
    if Pos(SecString[i], s1) > 0 then Exit;
    if Pos(SecString[i], CharSet) <= 0 then Exit;
  end; 
  s1 := CharSet;
  s2 := ''; 
  for i := 1 to Length(SecString) do
  begin
    x := Pos(SecString[i], s1);
    if x > 0 then s1 := Copy(s1, 1,x - 1) + Copy(s1, x + 1,Length(s1)); 
  end; 
  ss := SecString;
  for i := 1 to Length(Data) do
  begin
    s2 := s2 + ss[Ord(Data[i]) mod 16 + 1];
    ss := Copy(ss, Length(ss), 1) + Copy(ss, 1,Length(ss) - 1);
    s2 := s2 + ss[Ord(Data[i]) div 16 + 1];
    ss := Copy(ss, Length(ss), 1) + Copy(ss, 1,Length(ss) - 1); 
  end;
  Result := RNDString(s1, Random(MaxV - MinV) + minV + 1);
  for i := 1 to Length(s2) do Result := Result + s2[i] + RNDString(s1, Random(MaxV - MinV) + minV);
end; 

function Decode(Data, SecString, CharSet: string): string;
var
  i, x, x2: integer;
  s1, s2, ss: string;
begin
  Result := #1; 
  if Length(SecString) < 16 then Exit;
  for i := 1 to Length(SecString) do
  begin 
    s1 := Copy(SecString, i + 1,Length(SecString));
    if Pos(SecString[i], s1) > 0 then Exit;
    if Pos(SecString[i], CharSet) <= 0 then Exit;
  end; 
  s1 := CharSet;
  s2 := '';
  ss := SecString;
  for i := 1 to Length(Data) do if Pos(Data[i], ss) > 0 then s2 := s2 + Data[i]; 
  Data := s2; 
  s2   := ''; 
  if Length(Data) mod 2 <> 0 then Exit;
  for i := 0 to Length(Data) div 2 - 1 do 
  begin
    x := Pos(Data[i * 2 + 1], ss) - 1;
    if x < 0 then Exit;
    ss := Copy(ss, Length(ss), 1) + Copy(ss, 1,Length(ss) - 1); 
    x2 := Pos(Data[i * 2 + 2], ss) - 1;
    if x2 < 0 then Exit;
    x  := x + x2 * 16;
    s2 := s2 + chr(x);
    ss := Copy(ss, Length(ss), 1) + Copy(ss, 1,Length(ss) - 1);
  end;
  Result := s2;
end;



So yeah, if anyone could point me in the right direction I would greatly appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Converting functions from Delphi to C#

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 11:43 AM

Help us to help you. How did you fail miserably at the conversion? Is there an exception? If so what is the exception text and which line is it on? Are the functions behaving abnormally? If so what are they doing and what should they be doing? Are you getting screwy output? If so what are you inputting, what are you getting out, and what should you be getting?
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 12:20 PM

I as well suggest you post what you've tried. I'm not sure how many of us are Delphi experts. I know that I sure as hell am not. It'd help if you showed us your initial logic, so we might be able to suggest why it's broken.
Was This Post Helpful? 0
  • +
  • -

#4 Ionut   User is offline

  • D.I.C Lover
  • member icon

Reputation: 386
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 12:57 PM

I might help you with the conversion(as I am probably one of the last human beings programming with Delphi), but I have to know what went wrong. You don't find the corresponding functions in C#? post the code you have and I'll correct it.
Was This Post Helpful? 0
  • +
  • -

#5 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 01:06 PM

High five Ionut for Delphi!

To be honest if you really wrote that code which is perfectly possible and you have a basic knowledge of C# or even Java, you should at least be able to code the structure. We would be happy to help with any logic problems after we see some kind of effort from you.

And why don't you want to learn anything? Surely it would be beneficial to you (and us for that matter) if you actually took something out of the conversion?
Was This Post Helpful? 0
  • +
  • -

#6 Cantero   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-August 11

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 02:43 PM

Haha, I'm a tard, after running to the store and grabbing a monster I noticed the converter I used was switching Delphi's Copy code to C#'s Substring, but it reversed the values.

For example.
Copy(blahstring, 5, 10);

Would become....
blahstring.Substring(5, 10);

But needed to be...
blahstring.Substring(10, 5);

It also didn't take into account that in C# Substring uses indexes which start at 0, where in Delphi they start at 1.

Anywho, the finished code works fine now, here it is.

        public string RNDString(string Chars, int Count)
        {
            int i, x;
            string Result;

            Result = "";
            for (i = 0; i <= Count - 1; i++)
            {
                x = rand.Next(Chars.Length);
                Result = Result + Chars[x];
                Chars = Chars.Replace(Chars[x].ToString(), "");
            }
            return Result;
        }

        public string Encode(string Data, string SecString, string CharSet, int MinV = 0, int MaxV = 5)
        {
            int i, x;
            string s1, s2, ss;
            string Result;

            if (MinV > MaxV)
            {
                i = MinV;
                MinV = MaxV;
                MaxV = i;
            }
            if (MinV < 0) MinV = 0;
            if (MaxV > 100) MaxV = 100;
            Result = "";
            if (SecString.Length < 16) return "-1";

            s1 = CharSet;
            s2 = "";
            for (i = 0; i <= SecString.Length - 1; i++)
            {
                x = s1.IndexOf(SecString[i]);

                if (x > -1)
                {
                    s1 = s1.Replace(SecString[i].ToString(), "");
                }
            }

            ss = SecString;
            for (i = 0; i <= Data.Length - 1; i++)
            {
                s2 = s2 + ss[(int)Data[i] % 16 + 1];
                ss = ss.Substring(ss.Length - 1, 1) + ss.Substring(0, ss.Length - 1);
                s2 = s2 + ss[(int)Data[i] / 16 + 1];
                ss = ss.Substring(ss.Length - 1, 1) + ss.Substring(0, ss.Length - 1);
            }
            Result = RNDString(s1, rand.Next(MaxV - MinV) + MinV + 1);
            for (i = 0; i <= s2.Length - 1; i++)
            {
                Result = Result + s2[i] + RNDString(s1, rand.Next(MaxV - MinV) + MinV);
            }
            return Result;
        }

        public string Decode(string Data, string SecString, string CharSet)
        {
            int i, x, x2;
            string s1, s2, ss;
            string Result;

            Result = "1";
            if (SecString.Length < 16) return "-1";
            s1 = CharSet;
            s2 = "";
            ss = SecString;

            for (i = 0; i <= Data.Length - 1; i++)
            {
                if (ss.IndexOf(Data[i]) > -1)
                {
                    s2 = s2 + Data[i];
                }
            }

            Data = s2;
            s2 = "";
            if (Data.Length % 2 != 0) return "-1";

            for (i = 0; i <= Data.Length / 2 - 1; i++)
            {
                x = ss.IndexOf(Data[i * 2]);
                if (x < 0) break;
                ss = ss.Substring(ss.Length - 1, 1) + ss.Substring(0, ss.Length - 1);
                x2 = ss.IndexOf(Data[i * 2 + 1]);
                if (x2 < 0) break;
                x = x + x2 * 16 - 17;
                s2 = s2 + (Char)x;
                Debug.WriteLine(x);
                ss = ss.Substring(ss.Length - 1, 1) + ss.Substring(0, ss.Length - 1);
            }

            Result = s2;
            return Result;
        }



Also @Ryano, I never said I didn't want to learn anything, I was stating that it would be nice if someone could convert it for me, but that I wouldn't learn anything from that.

This post has been edited by Cantero: 26 August 2011 - 02:45 PM

Was This Post Helpful? 1
  • +
  • -

#7 Ryano121   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1461
  • View blog
  • Posts: 3,289
  • Joined: 30-January 11

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 03:06 PM

Ah I took that the wrong way then lol

Glad you got it working!
Was This Post Helpful? 0
  • +
  • -

#8 Cantero   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-August 11

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 03:12 PM

View PostRyano121, on 26 August 2011 - 03:06 PM, said:

Ah I took that the wrong way then lol

Glad you got it working!


Actually, going back and reading, it does kinda sound like I'm saying it would be nice to not learn anything. Haha.
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯°□°)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 04:15 PM

Thanks for coming back and sharing the solution. Nothing worse than finding a thread on the internet with a "I solved it" post and no actual resolution.
Was This Post Helpful? 0
  • +
  • -

#10 Cantero   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 26-August 11

Re: Converting functions from Delphi to C#

Posted 26 August 2011 - 05:38 PM

View PostCurtis Rutland, on 26 August 2011 - 04:15 PM, said:

Thanks for coming back and sharing the solution. Nothing worse than finding a thread on the internet with a "I solved it" post and no actual resolution.


Yes, those annoy the crap out of me!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1