c#字符串异或的问题

2025-01-08 12:53:11
推荐回答(4个)
回答1:

using System.Text;
using System;
namespace ConsoleTest{
public class Test{
static public void Main(String []args){
string s = "abcd";
StringBuilder b = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
b.Append(s[i] ^ 'k');
}
Console.WriteLine(b);
}
}
}


虽然能语法通过可运行,

但用C#的字符做异或操作,没有多少实际意义,

如果是涉及异或加密,必须在字节而不是字符上操作

回答2:

string s="abcd";你这样声明是个字符串,不是数组,所以不能像后面s[0]这样同过索引去获取。这样改一下string []s={a,b,c,d}

回答3:

using System;

class test {

static string xor(string s) {
char[] a = s.ToCharArray();
for(int i = 0; i < a.Length; i++)
a[i] = (char)(a[i] ^ 'k');
return new string(a);
}

static void Main()
{
string s = "abcd";
s = xor(s);
Console.WriteLine(s);
s = xor(s);
Console.WriteLine(s);
}
}

回答4:

学会调试吧.....这种东西....