简体   繁体   English

无法将C ++方法转换为C#

[英]Trouble converting C++ method to C#

I'm trying to translate a C++ method that uses a homegrown form of encryption. 我正在尝试转换使用本地加密形式的C ++方法。 But I don't understand what Buffer[x] and Input[x] are doing? 但是我不明白Buffer[x]Input[x]在做什么? As a C# developer (beginner at that), It looks like they should be arrays but they aren't declared as such. 作为C#开发人员(当时是初学者),看起来它们应该是数组,但并未这样声明。 Can anyone explain please? 谁能解释一下?

The input string "{x;ƒ~sq{j|tLtuq" translates to "MY SOFTWARE INC" 输入字符串“ {x;ƒ〜sq {j | tLtuq”“转换为” 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. 注意,我不知道encoded来源,所以我省略了。 The trick is to use a StringBuilder for your buffer so you can mutate the characters, another options would be a char array. 诀窍是为缓冲区使用StringBuilder ,以便您可以更改字符,另一个选项是char数组。

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. 但是,这会为输入“ {x;ƒ〜sq {j | tLtuq””输出“ MY TOFTWARE INC”,因此我不确定是字符串还是代码引起问题。

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" 输入“ {x;ƒ〜sq {j | tLtuq””,输出“ 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. 我不知道这是否有帮助,但是经过一些反向工程后,您可以使用以下错误数组(使用StringBuilder实现)来显示适当的信息。

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 . 它与c ++和c#中字符的编码方式有关,因此您可以尝试使用System.Text.Encoding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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