0

Is there anyone of you who could help me a bit with this? I have a small method in C#, that I would like to convert to PHP. Unfortunately my knowledge of PHP is very limited. I'm hoping someone could give me some clues of how to do this. Just to say it, I am not looking for someone to do it for me, just help me get started.

This is the C# method.

    private static string decode(string str1, string str2)
    {
        StringBuilder builder = new StringBuilder(str1);
        StringBuilder builder2 = new StringBuilder(str2);
        StringBuilder builder3 = new StringBuilder(str1.Length);
        int num = 0;
    Label_0084:
        while (num < builder.Length)
        {
            int num2 = 0;
            while (num2 < p1.Length)
            {
                if ((num == builder.Length) || (num2 == builder2.Length))
                {
                    goto Label_0084;
                }
                char ch = builder[num];
                char ch2 = builder2[num2];
                ch = (char)(ch ^ ch2);
                builder3.Append(ch);
                num2++;
                num++;
            }
        }
        return builder3.ToString();
    }

The two things I'm not at all sure about, and not even sure what to search for, is an equivialent of StringBuilder. Is there an equivialent? Or is it even needed? And then this part:

char ch = builder[num];
char ch2 = builder2[num2];
ch = (char)(ch ^ ch2);
builder3.Append(ch);

How would I replicate this in PHP?

Hope someone could help me shed some light over this. Best regards, /Rick

Rickard
  • 3,471
  • 2
  • 16
  • 5
  • [possible duplicate](http://stackoverflow.com/questions/124067/php-string-concatenation-performance) – Paolo Falabella Aug 22 '11 at 13:03
  • @Rickard, it might help if you annotated what was happening in your code. Not being familiar with C# myself, I couldn't help you because I don't know what `ch ^ ch2` does. – nickf Aug 22 '11 at 13:05
  • c# with a goto X( bare exceptional circumstance... baaaad. Maybe you will improve it converting it. – Paul C Aug 22 '11 at 13:10
  • @nickf - the same thing as in php, it's a XOR. – VolkerK Aug 22 '11 at 13:15

3 Answers3

0

The jsc project can convert .net/C# to PHP for you.

  1. Overview: http://jsc.sf.net
  2. Source: http://jsc.sourceforge.net/examples/web/OrcasAvalonWebApplication/
  3. Example: http://jsc.svn.sourceforge.net/viewvc/jsc/templates/OrcasAvalonWebApplication/

This sounds good to me but the accepted answer was:

I know you're hoping for someone who had experience but in case no one comes forward...

You might consider just copy and pasting the code into a PHP script and checking what breaks. Write a parser to fix that, run it across the entire script, and see what's the next thing that breaks. Continue until the script functions as expected.

If you're not using any of the more involved .Net classes I can't imagine you'll have too much trouble.

Source: How to convert code from C# to PHP

Community
  • 1
  • 1
Paul C
  • 4,687
  • 5
  • 39
  • 55
0

There's really no need for a StringBuilder in PHP. Just append strings like this:

$string = "start";
$string .= "appended string";
$string .= "appended string";

This is from the PHP manual with regards to accessing characters in a string

Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose.

So you can simply do this

$ch = $builder[$num];

Finally, you might want to check this post out concidering the xor stuff (ch1 ^ ch2): http://www.go4expert.com/forums/showthread.php?t=5555

As you can see, the syntax is just like in C#, except the cast stuff. I can't vouch for whether or not it will produce the exact same output, though.

havardhu
  • 3,576
  • 2
  • 30
  • 42
0

At first glance, this seems to be an XOR cipher. The one string provided is the key, and the other is a string which has been XOR'd by the key once before. XOR'ing it again with the key results in getting back the original string.

I would try to reimplement the code instead of copying it word for word, because it's not particularly well written (unless I am mistaken about the intent of the C# code, this can be rewritten in a much more readable 3 or 4 lines, without using goto, etc). There seem to be PHP examples out there on this type of thing.

Daniel B
  • 2,877
  • 18
  • 18