Home > Enterprise >  Perform XOR operation on 2 "Long" variables and display the alphanumeric result in .NET Co
Perform XOR operation on 2 "Long" variables and display the alphanumeric result in .NET Co

Time:04-26

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

CodePudding user response:

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