2

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.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Knoweldges
  • 72
  • 8
  • This is not a code conversion site. Post how **you** have tried to convert it and any specific issue you are having, or you will likely get downvoted and the question will be closed. – Evan L May 15 '14 at 17:12
  • Sorry I am new here :c. I will update my post with what I have tryed – Knoweldges May 15 '14 at 17:28
  • can you add example param1 input value and the resulting final value that is sent via the send method for those example values? I am assuming your param1 will actually be a numerical value, represented as a string? – Kaushal De Silva May 27 '14 at 04:27
  • param1 is what I want to send to the server, for exemple, on login I send "CxN#username#sha256(password)" – Knoweldges May 27 '14 at 06:36

1 Answers1

1

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);
    }
  • Ok, now another problem. On this line `this.socket.Send(this.MDT[bCMDTEC[0]] + this.MDT[bCMDTEC[1]] + this.MDT[bCMDTEC[2]] + this.MDT[bCMDTEC[3]] + Encode(textToSend));` where encode is just a wrapper for `Encoding.UTF8.GetBytes`. The compilator say me `"+" Operator cannot be used on int and byte[]` – Knoweldges May 27 '14 at 06:46
  • ah yes, sorry... you will need to cast each 'byte' from MDT to an int. I have editted my answer, please have a look at that line in the new answer; you will notice (int) casts in front of every MDT byte – Kaushal De Silva May 28 '14 at 00:08