8 bytes and their checksum

muslis
muslis used Ask the Experts™
on
I have 8 bytes:
17 08 19 09 09 0C 09 14

and when this bytes will counted in result will be 1F. Need explanation or some function (in c++) how from 8 bytes get 1F. I have one function, but i can't understand it.

Other examples:

8 bytes: 17 26 31 11 1D 01 0A 14
in result will be: 1B

8 bytes: 17 39 33 11 1D 01 0A 14
in result will be: 06
Byte Check_Sum_Message(Word Begin_Address,Word Count_Bytes)
              {
               Byte Value; 
               Word Count;
               Value=0;   
               for(Count=0;Count<Count_Bytes;Count++)
                {  
                 Value^=Rs_Buffer[Begin_Address+Count+1]; 
                }
               return(Value);
              }

          02 00 08 17 08 19 09 09 0C 09 14 1F
          Rs_Buffer[0]=02,Rs_Buffer[1]=00,Rs_Buffer[2]=08.......

          Size=0008;(Rs_Buffer[1],Rs_Buffer[2])
          Check_Sum=1F (Rs_Buffer[Size+3])

          1F=Check_Sum_Message(0,Size+2);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
"Value^=Rs_Buffer[Begin_Address+Count+1]; "

"^="   -->  Performs a bitwise combination of bitsets with the exclusive OR operation.

the bytes of your buffer was combined and the result (1F) is the checksum of your buffer

what is a checksum?
http://en.wikipedia.org/wiki/Checksum
In your code box, you indicate that if Size=8 and
Rs_Buffer = {0x2, 0x0, 0x8, 0x17, 0x8, 0x19, 0x9, 0x9, 0xC, 0x9, 0x14, 0x1F}
then Check_Sum_Message(0,Size+2) ==> Check_Sum_Message(0,10) yields 0x1F.

This is correct. In your Check_Sum_Message function, Count takes on values of 0,1,..9.
Since Begin_Address is 0, the index into the Rs_Buffer array (i.e., the expression Begin_Address+Count+1) takes on values of 1,2,...,10.

So, for some reason, you have this checksum function always ignoring the first byte of the Rs_Buffer array (i.e., an index of 0).

The XOR (^) operation for a bit is: 1^1 -> 0, 0^0 -> 0, 1^0 -> 1, and 0^1 -> 1. For two bytes, the XOR operation just applies the bit XOR operation on each corresponding bit of the two bytes.

So, the function is doing a XOR on the following bytes (indexed from 1..10):
        0^ 8^ 17^ 8^ 19^ 9^ 9^ C^ 9^ 14 -> 1F
(you can verify this by plugging this into a hex calculator)
(By the way, you can eliminate the factors 9^ 9 since any number XOR with itself is 0, and 0 XOR with any number is the number itself.)

Now, for the other Byte arrays that you mention in your question, you do not indicate what values you are using in the call to Check_Sum_Message. It cannot be the same call as noted above:
       Check_Sum_Message(0,Size+2) ==> Check_Sum_Message(0,10)
since in this function, the index would then exceed the max valid value of 7.

Below is your code tweaked to make it compile in C++:
typedef unsigned char Byte;
typedef unsigned long Word;

Byte Rs_Buffer3[] = {0x17, 0x08, 0x19, 0x09, 0x09, 0x0C, 0x09, 0x14};
Byte Rs_Buffer2[] = {0x17, 0x26, 0x31, 0x11, 0x1D, 0x01, 0x0A, 0x14 };
Byte Rs_Buffer1[] = {0x2, 0x0, 0x8, 0x17, 0x8, 0x19, 0x9, 0x9, 0xC, 0x9, 0x14, 0x1F};
Byte * Rs_Buffer;

Byte Check_Sum_Message(Word Begin_Address,Word Count_Bytes)
{
	Byte Value; 
	Word Count;
	Value=0;   
	for(Count=0;Count<Count_Bytes;Count++)
	{  
		Value^=Rs_Buffer[Begin_Address+Count+1]; 
	}
	return(Value);
}

#include <stdio.h>
int main() {
	Byte checkSum;
    Word Size = 8;
	Rs_Buffer = Rs_Buffer1;
	checkSum = Check_Sum_Message(0, Size+2);
	printf("Rs_Buffer1 checkSum = %#0x\n", checkSum);
}

Open in new window

A couple of things to notice.

The first Byte array in your question is
                  {17 08 19 09 09 0C 09 14}, which is a sub-string of the array in your code box:
   {02 00 08 17 08 19 09 09 0C 09 14 1F}

Also, note that the sub-string
                     {08 19 09 09 0C 09 14} when XOR together -> 0
I only mention this because XOR is associative and commutative, and thought this may help you or someone later to figure out how you are getting the answers for the other sub-strings.

Where are you getting these answers? Can you show your driver program that yields these answers?
Top Expert 2009

Commented:
It's already been mostly said, but it's as simple as :

0x00 XOR 0x08 XOR 0x17 XOR 0x08 XOR 0x19 XOR 0x09 XOR 0x09 XOR 0x0C XOR 0x09 XOR 0x14 = 0x1F
0x00 XOR 0x08 XOR 0x17 XOR 0x26 XOR 0x31 XOR 0x11 XOR 0x1D XOR 0x01 XOR 0x0A XOR 0x14 = 0x1B
0x00 XOR 0x08 XOR 0x17 XOR 0x39 XOR 0x33 XOR 0x11 XOR 0x1D XOR 0x01 XOR 0x0A XOR 0x14 = 0x06

Author

Commented:
Thank you for explanation. Function is working fine.

In other example of bytes i gave not full code, it had to be:
Byte Rs_Buffer3[] = {0x2, 0x0, 0x8, 0x17, 0x08, 0x19, 0x09, 0x09, 0x0C, 0x09, 0x14};
Byte Rs_Buffer2[] = {0x2, 0x0, 0x8, 0x17, 0x26, 0x31, 0x11, 0x1D, 0x01, 0x0A, 0x14 };

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial