Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XOR operation against byte values in C#

Status
Not open for further replies.

banswara

Programmer
Sep 27, 2002
16
CA
I need to do a checksum against a value being received by my program. I can simulate the operation by inputting the values into the Windows Calculator program in Scientific mode, using the XOR function, i.e. the hex byte value 33 XOR with the hex byte value 2A yields hex byte 19.

How would you code this operation in C#?

I've tried using the ^ operator, but it doesn't return the value that I expect...

Thanks in advance!
 
This ought to work.

Sample from MSDN:
Code:
// cs_operator_bitwise_OR.cs
using System;
class MainClass 
{
    static void Main() 
    {
        Console.WriteLine(true ^ false);  // logical exclusive-or
        Console.WriteLine(false ^ false); // logical exclusive-or
        // Bitwise exclusive-or:
        Console.WriteLine("0x{0:x}", 0xf8 ^ 0x3f);
    }
}


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Thanks for your response, I saw that example but am unclear on how to use it. My values are expressed as a byte which I've converted from a string using something similar to this:

//checkmessage is passed string for checksum operation

int n1 = Convert.ToInt32(checkmessage.Substring(4, 2), 16);
char c1 = (char)n1;
b1 = Convert.ToByte(c1);

I have 18 different bytes expressed as characters in the string, so I repeat the above operation on each to return a byte, then I do the following to get my final result:

br1 = Convert.ToByte( b1 ^ b2 ^ b3 ^ b4 ^ b5 ^ b6 ^ b7 ^ b8 ^ b9 ^ b10 ^ b11 ^ b12 ^ b13 ^ b14 ^ b15 ^ b16 ^ b17 ^ b18) ;

Why do I have to Convert.ToByte the result of my operation against the bytes? If I try the operation as follows:

br1 = b1 ^ b2 ^ b3... etc.

I get an error that it is not possible to implicitly convert from Int to Byte. Looking at the example it appears that the result of an operation with two bytes should be returned as a byte value, or am I missing something there?







 
Code:
int n1 = Convert.ToInt32(checkmessage.Substring(4, 2), 16);
char c1 = (char)n1;
b1 = Convert.ToByte(c1);
You probably ought to be using the ASCIIEncoding class to do this. It has a method which will return the bytes for a string. If your communications isn't using ASCII (possibly Unicode or EBCDIC), use the Encoder class for those encodings.

Once you have the byte array, you can loop thru it to build your XOR value. (this snippet assumes you want the XOR applied against the entire message)
Code:
byte[] b = ASCIIEncoding.GetBytes(checkmessage);
byte xorTotalByte = 0;
for(int i = 0; i < b.Length; i++)
   xorTotalByte ^= b[i];

Chip H.

____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Whoops. The GetBytes method isn't static. You'll need an instance of the ASCIIEncoder:
Code:
Encoding ae = Encoding.ASCII;
byte[] b = ae.GetBytes(checkmessage);
byte xorTotalByte = 0;
for(int i = 0; i < b.Length; i++)
   xorTotalByte ^= b[i];


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi Chip,

Your suggestions thus far have been very helpful and much appreciated. I think I'm close to getting this working.

However, I'm still not getting the expected result. I think maybe it is because the original string is ascii encoded, but uses 2 characters per byte.

For example the entire string is as follows:

022B3330300D0A303030303030360D0A303030342A03

The breakdown is:

02 2B 33 30 30 0D 0A 30 30 30 30 30 30 36 0D 0A 30 30 30 34 2A 03

(STX) (SIGN) (30.0) (SEPARATOR) (00 00 00.6) (SEPARATOR) (000.4) (CHECKSUM) (ETX)
The checksum value is passed to me from an outside provider and I am unable to change the calculation used to calculate it. It is calculated by doing the previously mentioned XOR against the HEX values represented in the ASCII stream, i.e. 33 HEX = 51 ASCII = the number/character 3. So, if I use the Windows calculator I enter 33 XOR 30 XOR 30 XOR 0D XOR 0A etc. When I finish, the result is the same as the checksum value that was passed to me, 2A.

The encoding.ascii method that you suggested appears to be working against one character at a time on the input string, so the result is different.

Again, any ideas/suggestions are most welcome.



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top