简体   繁体   中英

Trouble converting C++ method to C#

I'm trying to translate a C++ method that uses a homegrown form of encryption. But I don't understand what Buffer[x] and Input[x] are doing? As a C# developer (beginner at that), It looks like they should be arrays but they aren't declared as such. Can anyone explain please?

The input string "{x;ƒ~sq{j|tLtuq" translates to "MY SOFTWARE INC"

AnsiString __fastcall TMyMain::Decode(AnsiString Input)
{
  int error[] = { 2, 9, 5, 4, 1, 6, 7, 12, 19, 3, 1, 0, 21, 17 };
  int x;
  AnsiString Buffer = Input;

  if (encoded!=0)
  {
    int count = 0;

    for(x=Input.Length();x>=1;x--)
    {
      Buffer[x] = Input[x]-48+error[count];
      count++;
      if (count>=14)
        count=0;
    }
  }

  return Buffer;
}

Here's how I'd translate it. Note I have no idea where encoded comes from, so I left that out. The trick is to use a StringBuilder for your buffer so you can mutate the characters, another options would be a char array.

public static string Decode(string input)
{
    int[] error = { 2, 9, 5, 4, 1, 6, 7, 12, 19, 3, 1, 0, 21, 17 };
    StringBuilder buffer = new StringBuilder(input);

    int count = 0;
    for (int x = input.Length - 1; x >= 0; x--) {
        buffer[x] = (char)(input[x] - 48 + error[count]);
        count++;
        if (count >= 14)
            count = 0;
    }

    return buffer.ToString();
}

This however outputs "MY TOFTWARE INC" for the input "{x;ƒ~sq{j|tLtuq", so I'm not exactly sure if the issue is with your string or the code.

I've come up with this, but it doesn't quite return the expected results. Are you sure of the input string?

Input and output is string.

var encoded = true;
var input = "{x;ƒ~sq{j|tLtuq";
var output = Decode(input);
Console.WriteLine($"input \"{input}\", output \"{output}\"");

private static string Decode(string input)
{
    int[] error = { 2, 9, 5, 4, 1, 6, 7, 12, 19, 3, 1, 0, 21, 17 };
    var buffer = new char[input.Length];

    if (encoded)
    {
        int count = 0;

        for(var x=input.Length-1;x>=0;x--)
        {
            buffer[x] = (char) ((Convert.ToInt16(input[x])-48 + error[count]) & 0xFF);
            count++;
            if (count>=error.Length)
                count=0;
        }
        return new string(buffer)
    }
    return input;
}

Calling this writes the following to the console:

input "{x;ƒ~sq{j|tLtuq", output "MY bOFTWARE INC"

I don't know if it helps but after some reverse engeneering you could use the following errors array (with the StringBuilder implementation) to dispaly the proper information.

int[] error = { 2, 9, 5, 4, 1, 6, 7, 12, 19, 3, 1, -6, 21, 17 };//MY SOFTWARE INC

It has to do with how characters were encoded in c++ vs c# so you could try to play around with System.Text.Encoding .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM