Java Checksum Calculate checksum(String nmea)

Here you can find the source of checksum(String nmea)

Description

checksum

License

LGPL

Declaration

public static boolean checksum(String nmea) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static boolean checksum(String nmea) {
        int len = nmea.length();
        if (len < 3)
            return false;

        try {//w w  w.j av a  2 s . c  o  m
            String checksumString = nmea.substring(len - 2, len);
            int target = Integer.valueOf(checksumString, 16);
            int chk = computeChecksum(nmea);
            if (chk == target)
                return true;
        } catch (NumberFormatException e) {
            // System.out.println(e); e.printStackTrace();
        }

        return false;
    }

    public static int computeChecksum(String nmea) {
        char chk = 0;
        for (int i = 1; i < nmea.length(); i++) {
            char c = nmea.charAt(i);
            if (c == 0 || c == '*')
                break;
            chk ^= c;
        }
        return chk;
    }
}

Related

  1. checksum(File file)
  2. checksum(File file)
  3. checksum(InputStream is)
  4. checksum(InputStream is)
  5. checksum(String filename)
  6. checksum_icmpv6(byte pkt[])
  7. checksumBytesToString(byte[] digestBytes)
  8. checksumFile(File file)
  9. checksumLength(int algorithmCode, int dataLength)