Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to do XOR process in my window application. Suppose i have data like this:
XOR of (82 21 B3 00 00 01 DB 03 10 00 10) and i want to do this process.This data is in hexadecimal. How can i do this can any one solve this.
Posted

Use ^ for bitwise XOR in C#, for example:

C#
a = a ^ c;   (or simply:   a ^= c;)



Here is my Method for checksum calucation, have it

C#
static private byte CalCheckSum(byte[] _PacketData,int PacketLength)
       {
           Byte _CheckSumByte = 0x00;
           for (int i = 0; i < PacketLength; i++)
               _CheckSumByte ^= _PacketData[i];
           return _CheckSumByte;
       }


Thanks
--RA
 
Share this answer
 
v2
Comments
Rajesh Anuhya 2-Feb-12 2:35am    
My answer is updated
--RA
Sridhar Patnayak 2-Feb-12 2:41am    
Good one 5+
Rajesh Anuhya 2-Feb-12 2:42am    
Thanks Sridhar.
--RA
BillWoodruff 3-Feb-12 0:11am    
+5 excellent answer that has valuable educational content that goes "beyond" the OP's question: very useful !
Rajesh Anuhya 3-Feb-12 1:15am    
Thanks Bill
--RA
I hope this will solve ur Problem.

http://msdn.microsoft.com/en-us/library/zkacc7k1.aspx[^]


exapmle:
C#
Convert.ToString(0xf8 ^ 0x3f ^ 0x3f, 16);

//16 convert result to hex
 
Share this answer
 
v3
Comments
Rajesh Anuhya 2-Feb-12 21:17pm    
Edited: Code Tags added
--RA
You can XOR using the ^ symbol.
Try out something like
C#
int d1 = Convert.ToInt32(hex1, 16);
int d2 = Convert.ToInt32(hex2, 16);
int r = dec1 ^ dec2;
 
Share this answer
 
C#
class XOR
{
    static void Main()
    {
        // Logical exclusive-OR

        // When one operand is true and the other is false, exclusive-OR
        // returns True.
        Console.WriteLine(true ^ false);
        // When both operands are false, exclusive-OR returns False.
        Console.WriteLine(false ^ false);
        // When both operands are true, exclusive-OR returns False.
        Console.WriteLine(true ^ true);


        // Bitwise exclusive-OR

        // Bitwise exclusive-OR of 0 and 1 returns 1.
        Console.WriteLine("Bitwise result: {0}", Convert.ToString(0x0 ^ 0x1, 2));
        // Bitwise exclusive-OR of 0 and 0 returns 0.
        Console.WriteLine("Bitwise result: {0}", Convert.ToString(0x0 ^ 0x0, 2));
        // Bitwise exclusive-OR of 1 and 1 returns 0.
        Console.WriteLine("Bitwise result: {0}", Convert.ToString(0x1 ^ 0x1, 2));

        // With more than one digit, perform the exclusive-OR column by column.
        //    10
        //    11
        //    --
        //    01
        // Bitwise exclusive-OR of 10 (2) and 11 (3) returns 01 (1).
        Console.WriteLine("Bitwise result: {0}", Convert.ToString(0x2 ^ 0x3, 2));

        // Bitwise exclusive-OR of 101 (5) and 011 (3) returns 110 (6).
        Console.WriteLine("Bitwise result: {0}", Convert.ToString(0x5 ^ 0x3, 2));

        // Bitwise exclusive-OR of 1111 (decimal 15, hexadecimal F) and 0101 (5)
        // returns 1010 (decimal 10, hexadecimal A).
        Console.WriteLine("Bitwise result: {0}", Convert.ToString(0xf ^ 0x5, 2));

        // Finally, bitwise exclusive-OR of 11111000 (decimal 248, hexadecimal F8)
        // and 00111111 (decimal 63, hexadecimal 3F) returns 11000111, which is
        // 199 in decimal, C7 in hexadecimal.
        Console.WriteLine("Bitwise result: {0}", Convert.ToString(0xf8 ^ 0x3f, 2));
    }
}
 
Share this answer
 
You may use the following to achieve that:

C#
byte[] d = ...;
byte e = (byte)d.Aggregate(0, (a, b) => a ^ b);


Andi
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900