title: Doing math operations with bytes
body: <p>I need to do some math operations (+, - and mod) with bytes saved inside an array/packet. Practically I need to create a checksum for a packet using some specific ck rules (sum of all the bytes in the packet -> sum mod 100(256 in dec.) -> FD - mod). The problem is that all the automatic java conversion in integer creates wrong results (checked manually).</p>
  
  <p>How can I do this?</p>
  
  <p>EDIT: 
  thanks for the answers! 
  Added some rapid test code (sorry but I can't post the original code).</p>
  
  <p>I've just added the 0xFF mask in the for-cycle and the ck is right, but the other output are wrong. If you want to give me some more hints I'm here :D ready to learn!</p>
  
  <p>thanks</p>
  
  <p>the input:</p>
  
  <pre><code>    input byte[] packet = {(byte)0x80, (byte)0x11, (byte)0x30, (byte)0x02, (byte)0x46, (byte)0x56, (byte)0x57, (byte)0x30, (byte)0x38, (byte)0x30,
          (byte)0x35, (byte)0x45, (byte)0x52, (byte)0x00};
  </code></pre>
  
  <p>the ck calculator method:</p>
  
  <pre><code>  byte ckCalculator(byte[] b) {
  
      int len = b.length;
      int sum = 0;
  
      for(int i=0; i&lt;len; i++) {
          sum = sum + b[i]&amp;0xFF;      
      }
  
      System.out.println(String.format("%02X ", sum));
  
      int module = sum % 256;
  
      System.out.println(String.format("%02X ", module));
  
      int diff = 253 - module;
  
      byte ck = (byte)diff;
      System.out.println(String.format("%02X", ck));
      return ck;
  }
  </code></pre>
  
  <p>output:
  sum : 1A
  module: 1A
  <strong>ck : E3</strong></p>
  
  <p>expected output:
  sum: 31A
  module: 1A
  <strong>ck: E3</strong></p>