0

I have this line in my code and I can't seem to understand how it works -(in order to find my problem)

0- (277 in HEX mode 256)

I know the result should be 89

checksum = 0- (277(16) mode 256)

but I don't remember why

this is the C#

byte[] ByteMessage = {0x30 , 0x31 , 0x48 , 0x41 , 0x4E , 0X4F , 0X56 , 0X45 , 0X52 , 0X03 } 
byte answer = gen (ByteMessage )


     public static byte gen(byte[] p)
        {
            byte lcs = 0;
            foreach (byte b in p)
            {
                lcs += b;

            }

            lcs = (byte)(0 - lcs);
            return lcs;

        }
David12123
  • 119
  • 4
  • 15

1 Answers1

3

This 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 - lcs) performs negation modulo 256.

Since x + (-x) = 0 (by definition) even in mod-256 arithmetic, this checksum has the property that if it is put together with the data, the checksum of that combination is zero.

harold
  • 61,398
  • 6
  • 86
  • 164
  • 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 Oct 09 '18 at 12:34
  • @David12123 it's not just modulo 256 but also a negation, -0x277 mod 256 is 0x89 – harold Oct 09 '18 at 12:36
  • 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. – harold Oct 09 '18 at 12:49
  • 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 – David12123 Oct 09 '18 at 12:49
  • 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. – harold Oct 09 '18 at 12:51
  • 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 , – David12123 Oct 09 '18 at 13:03
  • @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. – harold Oct 09 '18 at 13:12
  • 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? – David12123 Oct 09 '18 at 14:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181560/discussion-between-harold-and-david12123). – harold Oct 09 '18 at 14:44