C#中的异或字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14971836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:36:43  来源:igfitidea点击:

XOR-ing strings in C#

c#xor

提问by

I recently started playing around with C#, and I'm trying to understand why the following code doesn't compile. On the line with the error comment, I get:

我最近开始玩 C#,我试图理解为什么下面的代码不能编译。在带有错误注释的行上,我得到:

Cannot implicitly convert type 'int' to 'char'. An explicit conversion exits (are you missing a cast?)

无法将类型“int”隐式转换为“char”。显式转换退出(您是否缺少演员表?)

I'm trying to do a simple XOR operation with two strings.

我正在尝试对两个字符串进行简单的 XOR 操作。

public string calcXor (string a, string b)
{
    char[] charAArray = a.ToCharArray();
    char[] charBArray = b.ToCharArray();
    char[] result = new char[6];
    int len = 0;

    // Set length to be the length of the shorter string
    if (a.Length > b.Length)
        len = b.Length - 1;
    else
        len = a.Length - 1;

    for (int i = 0; i < len; i++) {
        result[i] = charAArray[i] ^ charBArray[i]; // Error here
    }

    return new string (result);
}

采纳答案by allen

You are doing an xoron 2 characters. This will do an implicit type conversion to intfor you since there is no data loss. However, converting back from intto charwill need you to provide an explicit cast.

你正在做一个xor2 个字符。这将为您进行隐式类型转换,int因为没有数据丢失。但是,从intto转换回char将需要您提供显式转换。

You need to explicitly convert your intto a charfor result[i]:

您需要明确地将您的转换int为 a charfor result[i]

result[i] = (char) (charAArray[i] ^ charBArray[i]);

回答by Ton Snoei

If you use XOR-ing to hide data, take a look at the code below. The key will be repeated as long as necessary. It is maybe a shorter/better approach:

如果您使用 XOR-ing 来隐藏数据,请查看下面的代码。只要需要,密钥将被重复。这可能是一种更短/更好的方法:

public static string xorIt(string key, string input)
{
    StringBuilder sb = new StringBuilder();
    for(int i=0; i < input.Length; i++)
        sb.Append((char)(input[i] ^ key[(i % key.Length)]));
    String result = sb.ToString ();

    return result;
}

回答by mckenzm

If the value of the result is important then Allan is correct (accepted answer). If you are just looking for a match and are not worried about performance, use strcmp() or memcmp() as an alternative.

如果结果的值很重要,那么 Allan 是正确的(接受的答案)。如果您只是在寻找匹配项并且不担心性能,请使用 strcmp() 或 memcmp() 作为替代方案。

In assembler is it common to initialize things by an XOR against themselves as the T-cycles are fewer. If I was brute-forcing (hash compare), then this would be an improvement over strcmp or memcmp as I really don't sort order, just match/nomatch.

在汇编程序中,由于 T 循环较少,因此通过对自身进行异或来初始化事物是很常见的。如果我是蛮力(哈希比较),那么这将是对 strcmp 或 memcmp 的改进,因为我真的不排序顺序,只是匹配/不匹配。

Readers should also be aware of thiswhich can be tweaked.

读者也应该意识到一点可以调整。