last day (15 days later) » 

14:44
2
A: mode 256 exaplin

haroldThis is a simple checksum. The math behind is this: gen(data) = -sum(data) mod 256 Since (a + b) mod c = ((a mod c) + (b mod c)) mod c the intermediate sums can already be reduced modulo 256. Casting to a byte implicitly performs modulo 256, and so does += between two bytes. Finally (byte)(0 ...

OK , but the calculation doesn't add up - the sum is 277 (hex) or 631(Dec) , mod so how did it get to 89 (hex) or 137 (dec) ?
@David12123 it's not just modulo 256 but also a negation, -0x277 mod 256 is 0x89
By the way be careful with the % operator in C#, for signed integers it means remainder and not really modulo: the result can be negative. In this case you can easily cast to byte of course. Or use & 0xFF which comes down to the same thing but without changing the type.
I'm sorry - I can't see it , can you show me how? I have try in the calculator and also with a pencil .... don't get it
Ok let's first take 0x277 mod 256, in other words, take the botton 8 bits: 0x77. Written in binary: 01110111. Apply negation, with -x = ~x + 1: ~01110111 + 1 = 10001000 + 1 = 10001001 which is 0x89 in hexadecimal.
can you show me in HEX only ? this is what I understand : 277 mode 256 ==> 277 mode 100==> 277-100=177==>177-100=77. with this I need to work. ==>but how from 0-77 you get 89? (all in HEX)? Thanks ,
14:44
@David12123 ok but conversion between binary and hex is trivial. Anyway, 00 - 77: subtract 7 from 0, we get 9 and a borrow. Subtract 7 from 0 again but with borrow, now we get 8 and a borrow. So the result is 89. Also a borrow out of the top, just ignore that as usual.
still something isn't working for me : when I do the CheckSum for this byte Testing [] = {0x30 , 0x35 , 0x5C , 0x73 , 0x54 , 0x44 , 0xF4 , 0xE2 , 0x5C , 0x3B , 0x03};// I get C4 (which is the correct ) but when I try to divide to by taking every (0x30,0x35 mod +0x73,0x54 mod + 0x44,0xf4 mode + 0xe2,0x5c mode + 0x3b,0x03 mod) mod - I get B4 , why?
I don't understand what you did
hi and thank you
I have a this byte array byte Testing [] = {0x30 , 0x35 , 0x5C , 0x73 , 0x54 , 0x44 , 0xF4 , 0xE2 , 0x5C , 0x3B , 0x03};//example meesage
when I run the function I get result of C4 . which is correct
I made a little program that shows the intermediate results: ideone.com/5cKR3u
when I try to make the same thing but in parts I get B0
14:50
at which step does it diverge from the output of the program I just showed?
I wanted to know how it's works because I need to calculate it by parts , but it doesn't wok for me - is there any way to show you what I did ?
byte MessageStart [] = {0x02};
byte Address [] = {0x30 , 0x35};
byte Next [] = {0x5C , 0x73};
byte tmpMsg [] = {0x54 , 0x44 , 0xF4 , 0xE2};
byte BeforeEnd [] = {0x5C , 0x3B};
byte MessageEnd [] = {0x03};
maybe a picture of your paper if you did it on paper or type up all the steps maybe
I have the function on every one of the arrays , and got this :
155
49
146
105
253
is that decimal now?
OK 253 is -3 that one I can do in my head
no , I have try to print it as DEC so it will be easy for me to see it
14:56
155 = 9B seems good
-(0x5C + 0x73) & 0xff == 49 also good
-(0x54 + 0x44 + 0xF4 + 0xE2) & 0xff == 146 good
-(0x5C + 0x3B) & 0xff == 105 also good
no problems here
this is what I thought
but now what to do?
well what do you want to do? combine them?
yes , like you said here (a + b) mod c = ((a mod c) + (b mod c)) mod c
ok yes, a slightly different rule applies here though since there is negation too
but -(-a + -b) = a + b so we can in fact sum the individual checksums
so this was my problem....
15:01
155+49+146+105+253 & 0xff == 0xc4
let me check in my code
I think my problem is on the adding
byte CK [5] = {0};
byte CheckSum1 = gen( Address, sizeof(Address) ); //getting the check sum of it
Serial.println("The Check Sum is Address - " );
Serial.println(CheckSum1 );
CK[0] = (CheckSum1 , HEX);

CheckSum1 = {0};
CheckSum1 = gen( Next, sizeof(Next) ); //getting the check sum of it
Serial.println("The Check Sum is Next - " );
Serial.println(CheckSum1);
CK[1] = (CheckSum1, HEX);

CheckSum1 = {0};
CheckSum1 = gen( tmpMsg, sizeof(tmpMsg) ); //getting the check sum of it
Serial.println("The Check Sum is tmpMsg - " );
byte David = 0;
byte DavidArray [1] = {0};

for (int i = 0 ; i < sizeof(CK); i++)
{
Serial.print (i + "-");
Serial.println(CK[i]);
David = David + (CK[i]);

}
but the david is only -16 ???? , how could it be ?
I don't know, it got more confusing now
:-) I know....
I took the hex value of each part and put it into byte array called CK
is this still C#?
it looks more C++ish
no this is c++ (arduino) -
15:13
what about CK[0] = (CheckSum1 , HEX); what does this really do
it should put the value as hex in the array
isn't this a sneaky comma operator, so it only puts (byte)HEX in the array?
no it doesn't so - i remove it now , and now I can see when I'm adding all the array I get 196 -->which is C4
155
204
94
199
196
is it OK , or just Coincidence
?
15:22
this is the same output: https://ideone.com/YKBjop
so it seems good
great! thank you for the help!
I will give it more message to see if this OK
Thanks again !

last day (15 days later) »