title: Need Assistance in Calculating Checksum
body: <p>I am working on an interface in Python to a home automation system (ElkM1).   I have sample code in C# below which apparently correctly calculates the checksum needed when sending messages to this system.   I put together the python code below but it doesn't appear to be returning the correct value.</p>
  
  <p>According to the documentation the checksum of the message needs to be the sum of the ASCII values of the message in mod256 then taken as 2s complement.  From their manual: "This is the hexadecimal two‟s complement of the modulo-256 sum of the ASCII values of all characters in the message excluding the checksum itself and the CR-LF terminator at the end of the message. Permissible characters are ASCII 0-9 and upper case A-F. When all the characters are added to the Checksum, the value should equal 0."</p>
  
  <p>The vendor has a tool which will calculate the correct checksum.   As test data I have been using '00300005000' which should return a checksum of 74</p>
  
  <p>My code returns 18</p>
  
  <p>Thanks in advance.</p>
  
  <h1>My Code (Python)</h1>
  
  <pre><code>def calc_checksum (string):
      '''
      Calculates checksum for sending commands to the ELKM1.  
      Sums the ASCII character values mod256 and takes
      the Twos complement
      '''
      sum= 0
  
      for i in range(len(string)) :
          sum = sum + ord(string[i])
  
      temp = sum % 256  #mod256
      rem = temp ^ 256  #inverse
      cc1 = hex(rem)
      cc = cc1.upper()
      p=len(cc)
      return cc[p-2:p]
  </code></pre>
  
  <h1>Their Code C#:</h1>
  
  <pre class="lang-csharp prettyprint-override"><code>private string checksum(string s)
  {
      int sum = 0;
      foreach (char c in s)
          sum += (int)c;
  
      sum = -(sum % 256);
  
      return ((byte)sum).ToString("X2");
  }
  </code></pre>