帮助我修复我的代码无法将其从Java转换为C#

时间:2019-02-09 05:02:32

标签: c#

我有java的thies代码来解密此刺
575-371-46b-576-362-276-528-634-731-330-53c-729-436

10:26:00.358 [main] INFO reactor.Flux.FlatMap.3 - request(unbounded)
10:26:00.591 [main] INFO reactor.Flux.FlatMap.3 - onNext(Pair[foo,5])
10:26:00.593 [main] INFO reactor.Flux.FlatMap.3 - onNext(Pair[bar,0])
10:26:00.594 [main] INFO reactor.Flux.FlatMap.3 - onNext(Pair[baz,5])
10:26:00.594 [main] INFO reactor.Flux.FlatMap.3 - onNext(Pair[foobar,0])
10:26:00.594 [main] INFO reactor.Flux.FlatMap.3 - onComplete()

i托盘转换为c#

public static String decrypt(String str) {
    str = str.replace("-", "");
    String result = "";
    for (int i = 0; i < str.length(); i += 3) {
        result = result + ((char) (Integer.parseInt(str.substring(i + 1, i + 3), 16) ^ Integer.parseInt(String.valueOf(str.charAt(i)))));
    }
    return result;
}

但是我没有成功解密字符串 我得到了很多erorr,但我仍然是C#的入门者,我无法解决它

private void button1_Click(object sender, EventArgs e)
{
String str= "575-371-46b-576-362-276-528-634-731-330-53c-729-436";
str = str.Replace("-", "");
String result = "";
for (int i = 0; (i < str.Length); i += 3)
{
result = (result + ((char)((int.Parse(str.Substring((i + 1), (i + 3)), 16) | int.Parse(String.valueOf(str[i]))))));

}
textBox1.Text = result; }

2 个答案:

答案 0 :(得分:1)

这是您要查找的C#:

npm install -g composer-cli@0.x.x

但是,这有点可怕。

我希望在C#中使用它:

result += (char)(int.Parse(str.Substring(i + 1, 2), System.Globalization.NumberStyles.HexNumber) ^ int.Parse(str[i].ToString()));

甚至:

var characters =
    str
        .Split('-')
        .Select(x =>
            int.Parse(x.Substring(1, 2), System.Globalization.NumberStyles.HexNumber) ^ (x[0] - '0'))
        .Aggregate("", (a, c) => a + (char)c);

答案 1 :(得分:0)

此问题已具有批准的答案。但是,看到原始Java代码的效率低下(例如添加字符串),我想提出自己的C#稍有改进的版本。去吧:

private static int ParseXInt(string x, int i, int n) =>
    int.Parse(x.Substring(i, n),
              System.Globalization.NumberStyles.HexNumber);

public static string Decrypt(string s) =>
    new string(s.Split('-')
                .Select(x => (char)(ParseXInt(x, 0, 1) ^
                                    ParseXInt(x, 1, 2)))
                .ToArray());

简短,可读,合理高效(尽管使用LINQ),并且(我认为)优雅。