title: NMEA checksum calculation
body: <p>I have a problem with calculating the checksum for NMEA sentences. I am using the following java code:</p>
  
  <pre><code>private static String getSum(String in) {
      int checksum = 0;
      if (in.startsWith("$")) {
          in = in.substring(1, in.length());
      }
  
      int end = in.indexOf('*');
      if (end == -1)
          end = in.length();
      for (int i = 0; i &lt; end; i++) {
          checksum = checksum ^ in.charAt(i);
      }
      String hex = Integer.toHexString(checksum);
      if (hex.length() == 1)
          hex = "0" + hex;
      return hex.toUpperCase();
  }
  </code></pre>
  
  <p>This code is similar to many other examples around the internet and everything works fine until I try a sentence like this..</p>
  
  <p><code>$PSRF101,-2686700,-4304200,3851624,96000,497260,921,12,3*1C</code></p>
  
  <p>This sentence is from the <a href="http://www.sparkfun.com/datasheets/GPS/NMEA%20Reference%20Manual-Rev2.1-Dec07.pdf" rel="noreferrer">NMEA Reference Manual</a> and so I assume the checksum will be correct. But when I calculate it, I get *2F as the checksum and not 1C.</p>
  
  <p>I think this is because of the negative values in the sentence, but I have no clue how to deal with them. Does anybody have a suggestion?</p>