title: How to compute "binary values addition of characters" for checksum
body: <p>Working in Java, here is the specification i have in order to implement a checksum calculation on character messages:</p>
  
  <pre><code>    8.3.3 Checksum—The checksum permits the receiver to detect a defective frame. The checksum is encoded as two characters which are sent after the &lt;ETB&gt; or &lt;ETX&gt; character. The checksum is computed by adding the  binary values of the characters, keeping the least significant eight bits of the result.
      8.3.3.1 The checksum is initialized to zero with the &lt;STX&gt; character. The first character used in computing the checksum is the frame number. Each character in the message text is added to the checksum (modulo 256). The computation for the checksum does not include &lt;STX&gt;, the checksum characters, or the trailing &lt;CR&gt; and &lt;LF&gt;.
      8.3.3.2 The checksum is an integer represented by eight bits, it can be considered as two groups of four bits. The groups of four bits are converted to the ASCII characters of the hexadecimal representation. The two ASCII characters are transmitted as the checksum, with the most significant character first.
      8.3.3.3 For example, a checksum of 122 can be represented as 01111010 in binary or 7A in hexadecimal. The checksum is transmitted as the ASCII character 7 followed by the character A.
  </code></pre>
  
  <p>Here is what i have understand and implemented, but it doesn't seem to be working... :</p>
  
  <pre><code>    private void computeAndAddChecksum(byte[] bytes, OutputStream outputStream) {
      logBytesAsBinary(bytes);
      long checksum = 0;
      for (int i = 0; i &lt; bytes.length; i++) {
          checksum += (bytes[i] &amp; 0xffffffffL);
              }
              int integerChecksum = (int)checksum;
      String hexChecksum = Integer.toHexString(integerChecksum).toUpperCase();
      logger.info("Checksum for "+new String(bytes)+" is "+checksum+" in hexa: "+hexChecksum);
      try {
          if (outputStream != null)
          {
              outputStream.write(hexChecksum.getBytes());
          }
      } catch (IOException e) {
          logger.error(e.getMessage());
      }
  }
  </code></pre>
  
  <p>Do you any idea why this snippet is not adapted to the specification ?
  Here is an example i was given if it could help:</p>
  
  <pre><code>    &lt;STX&gt;3L|1&lt;CR&gt;&lt;ETX&gt;3C&lt;CR&gt;&lt;LF&gt;
  </code></pre>
  
  <p>so the checksum of </p>
  
  <pre><code>    3L|1&lt;CR&gt;&lt;ETX&gt;
  </code></pre>
  
  <p>should be</p>
  
  <pre><code>    3C
  </code></pre>
  
  <p>Thank you very much for you help.</p>