对 2 "Long" 个变量执行 XOR 运算并在 .NET Core 控制台应用程序中显示字母数字结果

Perform XOR operation on 2 "Long" variables and display the alphanumeric result in .NET Core Console Application

我正在构建一个 .NET Core 控制台应用程序,我试图在其中对 2 个“Long”数据类型值执行“XOR”操作。我想要的结果应该是字母数字,如“21341FAK234A213KR”,但我得到的结果是整数。

对 2 个“Long”变量执行 XOR 运算并显示字母数字结果?

代码

using System.Text;

Console.WriteLine("Please enter your PAN Number: ");
long PAN = long.Parse(Console.ReadLine());

Console.WriteLine("Please enter your PIN Number: ");
long PIN = long.Parse(Console.ReadLine());

long PinBlock()
{
    // PAN Code without first 3 characters and last character
    string str1 = Convert.ToString(PAN).Remove(0,3);
    string str2 = str1.Remove(str1.Length - 1, 1);

    // Adding 4 0s at the start of the remaining PAN Number 
    int count = 4;
    char someChar = '0';
    string AlgoA = str2.PadLeft(count + str2.Length, someChar);
    Console.WriteLine("ALgoA: " + AlgoA);

    //Finding the length of the pin code and adding it to the pin code wth 10 'F's
    string PinLength = Convert.ToString(PIN);
    PinLength = PinLength.ToString();
    string result = String.Format("{0,2:X2}", PinLength.Length);
    string AlgoB = result + PIN + "FFFFFFFFFF";
    Console.WriteLine("ALgoB: " + AlgoB);

    byte[] AlgoAbytes = Encoding.ASCII.GetBytes(AlgoA);
    long AlgoAlong = BitConverter.ToInt64(AlgoAbytes);
    Console.WriteLine("ALgoAlong: " + AlgoAlong);

    byte[] AlgoBbytes = Encoding.ASCII.GetBytes(AlgoB);
    long AlgoBlong = BitConverter.ToInt64(AlgoBbytes);
     Console.WriteLine("ALgoBlong: " + AlgoBlong);

    long PinBlock = AlgoAlong ^ AlgoBlong;
    return PinBlock;
    
}

Console.WriteLine("XOR of 2 long Values: " + PinBlock());

我找到了答案。我没有将它们转换成位,而是将它们转换成十六进制。这是代码:

Console.WriteLine("Please enter your PAN Number: ");
long PAN = long.Parse(Console.ReadLine());

Console.WriteLine("Please enter your PIN Number: ");
long PIN = long.Parse(Console.ReadLine());

string PinBlock()
{
    // PAN Code without first 3 characters and last character
    string str1 = Convert.ToString(PAN).Remove(0,3);
    string str2 = str1.Remove(str1.Length - 1, 1);

    // Adding 4 0s at the start of the remaining PAN Number 
    int count = 4;
    char someChar = '0';
    string AlgoA = str2.PadLeft(count + str2.Length, someChar);
    Console.WriteLine("ALgoA: " + AlgoA);

    //Finding the length of the pin code and adding it to the pin code wth 10 'F's
    string PinLength = Convert.ToString(PIN);
    PinLength = PinLength.ToString();
    string result = String.Format("{0,2:X2}", PinLength.Length);
    string AlgoB = result + PIN + "FFFFFFFFFF";
    Console.WriteLine("ALgoB: " + AlgoB);

    long dec1 = Convert.ToInt64(AlgoA, 16);
    long dec2 = Convert.ToInt64(AlgoB, 16);
    long res = dec1 ^ dec2;
    string hexResult = res.ToString("X");
    //byte[] AlgoAbytes = Encoding.ASCII.GetBytes(AlgoA);
    //long AlgoAlong = BitConverter.ToInt64(AlgoAbytes);
    //Console.WriteLine("ALgoAlong: " + AlgoAlong);

    //byte[] AlgoBbytes = Encoding.ASCII.GetBytes(AlgoB);
    //long AlgoBlong = BitConverter.ToInt64(AlgoBbytes);
    // Console.WriteLine("ALgoBlong: " + AlgoBlong);

    //long PinBlock = AlgoAlong ^ AlgoBlong;
    //return PinBlock.ToString();
    return hexResult;

}

Console.WriteLine("XOR of 2 long Values: " + PinBlock());