Knoweldges
Knoweldges

Reputation: 72

Translate ActionScript 3 code I've wrote to C#

recently I have discovered C#, which is really what I want. Before C# I was coding with AS3. I've recoded all my old program using C# but I am blocked with this :

public function Envoie_Serveur(param1:String) : void
{
    var _loc_2:* = String(this.CMDTEC % 9000 + 1000).split("");
    this.Serveur.send(this.MDT[_loc_2[0]] + this.MDT[_loc_2[1]] + this.MDT[_loc_2[2]] + this.MDT[_loc_2[3]] + param1);
    var _loc_3:* = this;
    var _loc_4:* = this.CMDTEC + 1;
    _loc_3.CMDTEC = _loc_4;
    return;
}

CMDTEC and MDT are 2 byteArray (byte[] in C# I guess)

That is what I have tried but which is not working ;c

byte[] _loc_1 = Encode((Int64.Parse(this.CMDTEC[0].ToString("X", System.Globalization.NumberStyles.HexNumber)) % 9000 + 1000) + "");
var fingerprint = new byte[4];

fingerprint[0] = byte.Parse(this.MDT[_loc_1[0]].ToString("X"), System.Globalization.NumberStyles.HexNumber);
fingerprint[1] = byte.Parse(this.MDT[_loc_1[1]].ToString("X"), System.Globalization.NumberStyles.HexNumber);
fingerprint[2] = byte.Parse(this.MDT[_loc_1[2]].ToString("X"), System.Globalization.NumberStyles.HexNumber);
fingerprint[3] = byte.Parse(this.MDT[_loc_1[3]].ToString("X"), System.Globalization.NumberStyles.HexNumber);
this.CMDTEC++;

And for exemple, that is what CMDTEC and MDT contains :

this.MDT = "1400175151406"; (just for exemple, I get this by socket)
this.CMDTEC = "8306"; (idem as ^)

How can I convert properly that to C# please ? Thanks in advance for answers.

Upvotes: 2

Views: 945

Answers (1)

Kaushal De Silva
Kaushal De Silva

Reputation: 592

Here's an attempt; but you need to add more details to your question regarding inputs and outputs, datatypes etc. Although you are dealing with strings, it appears you are mainly handling numeric values.

The code below is verbose for clarity, it can be condensed a lot more. Please note I haven't actually compiled and tried the code (because I don't have a Serveur object, it won't compile for me).

    byte[] MDT = System.Text.Encoding.ASCII.GetBytes ("1400175151406");
    byte[] CMDTEC = System.Text.Encoding.ASCII.GetBytes ("8306");

    void Envoie_Serveur(string param1)
    {
        // firstly, get CMDTEC as a string, assuming ascii encoded bytes
        string sCMDTEC = System.Text.Encoding.ASCII.GetString(CMDTEC);

        // now convert CMDTEC string to an int
        int iCMDTEC = int.Parse(sCMDTEC);

        // now do modulation etc on the int value
        iCMDTEC = iCMDTEC % 9000 + 1000;

        // now convert modulated int back into a string
        sCMDTEC = iCMDTEC.ToString();

        // now convert modulated string back to byte array, assuming ascii encoded bytes
        byte[] bCMDTEC = System.Text.Encoding.ASCII.GetBytes(sCMDTEC);

        // send the data
        this.Serveur.send(((int)this.MDT[bCMDTEC[0]]) + ((int)this.MDT[bCMDTEC[1]]) + ((int)this.MDT[bCMDTEC[2]]) + ((int)this.MDT[bCMDTEC[3]]) + int.Parse(param1));

        // convert CMDTEC bytes to string again
        sCMDTEC = System.Text.Encoding.ASCII.GetString(CMDTEC);

        // convert CMDTEC string to int again
        iCMDTEC = int.Parse(sCMDTEC);

        // increament CMDTEC
        iCMDTEC += 1;

        // convert back to string
        sCMDTEC = iCMDTEC.ToString();

        // convert back to bytes
        this.CMDTEC = System.Text.Encoding.ASCII.GetBytes(sCMDTEC);
    }

Upvotes: 1

Related Questions