title: Checksum calculation - two’s complement sum of all bytes
body: <p>I have instructions on creating a checksum of a message described like this:</p>
  
  <p>The checksum consists of a single byte equal to the two’s complement sum of all bytes starting from the “message type” word up to the end of the message block (excluding the transmitted checksum). Carry from the most significant bit is ignored.</p>
  
  <p>Another description I found was:
  The checksum value contains the twos complement of the modulo 256 sum of the other words in the data message (i.e., message type, message length, and data words). The receiving equipment may calculate the modulo 256 sum of the received words and add this sum to the received checksum word. A result of zero generally indicates that the message was correctly received. </p>
  
  <p>I understand this to mean that I sum the value of all bytes in message (excl checksum), get modulo 256 of this number.  get twos complement of this number and that is my checksum.</p>
  
  <p>But I am having trouble with an example message example (from design doc so I must assume it has been encoded correctly).</p>
  
  <pre><code>unsigned char arr[] = {0x80,0x15,0x1,0x8,0x30,0x33,0x31,0x35,0x31,0x30,0x33,0x30,0x2,0x8,0x30,0x33,0x35,0x31,0x2d,0x33,0x32,0x31,0x30,0xe};
  </code></pre>
  
  <p>So the last byte, 0xE, is the checksum.  My code to calculate the checksum is as follows:</p>
  
  <pre><code>bool isMsgValid(unsigned char arr[], int len) {
     int sum = 0;
     for(int i = 0; i &lt; (len-1); ++i) {
        sum += arr[i];
     }
     //modulo 256 sum
     sum %= 256;
  
     char ch = sum;
  
     //twos complement
     unsigned char twoscompl = ~ch + 1;
  
     return arr[len-1] == twoscompl;
  }
  
  
  int main(int argc, char* argv[])
  {
     unsigned char arr[] = {0x80,0x15,0x1,0x8,0x30,0x33,0x31,0x35,0x31,0x30,0x33,0x30,0x2,0x8,0x30,0x33,0x35,0x31,0x2d,0x33,0x32,0x31,0x30,0xe};
     int arrsize = sizeof(arr) / sizeof(arr[0]);
  
     bool ret = isMsgValid(arr, arrsize);
  
     return 0;
  }
  </code></pre>
  
  <p>The spec is here:=  <a href="http://www.sinet.bt.com/227v3p5.pdf" rel="nofollow">http://www.sinet.bt.com/227v3p5.pdf</a></p>
  
  <p>I assume I have misunderstood the algorithm required.  Any idea how to create this checksum?</p>
  
  <p>Flippin spec writer made a mistake in their data example.  Just spotted this then came back on here and found others spotted too.  Sorry if I wasted your time.  I will study responses because it looks like some useful comments for improving my code.</p>