在2“ long”上执行XOR操作变量并在.NET Core Console应用程序中显示字母数字结果

发布于 2025-01-23 08:19:18 字数 1589 浏览 5 评论 0原文

我正在构建一个.NET Core Console应用程序,其中我正在尝试在2“长”数据类型值上执行“ XOR”操作。我想要的结果应该是字母数字,例如“ 21341fak234a213kr”,但我会得到整数的结果。

在2个“长”变量上执行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());

I'm building a .NET Core console application in which I'm trying to perform an "XOR" operation on 2 "Long" datatype values. The result I want should be alphanumeric like "21341FAK234A213KR", but I'm getting results as integers.

Perform XOR operation on 2 "Long" variables and display the alphanumeric result?

CODE

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());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

预谋 2025-01-30 08:19:18

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

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());

I found the answer. Rather than converting them into bits, I converted them into Hexadecimal. Here's the code:

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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文