Skip to content

Instantly share code, notes, and snippets.

@aeppert
Last active January 20, 2017 15:31
Show Gist options
  • Select an option

  • Save aeppert/9b31e0b5ce411523dd3327335213e4fd to your computer and use it in GitHub Desktop.

Select an option

Save aeppert/9b31e0b5ce411523dd3327335213e4fd to your computer and use it in GitHub Desktop.
#include <sstream>
#include <iostream>
#include <numeric>
#include <string>
#include <iostream>
std::string &righttrim(std::string &s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
char ck(char sum, char ch)
{
return sum ^ ch;
}
bool nmea_checksum(std::string sentence)
{
std::string trimmed_sentence = righttrim(sentence);
char verify = (char)std::stoul(trimmed_sentence.substr(trimmed_sentence.find("*")+1, trimmed_sentence.length()), NULL, 16);
char check = std::accumulate(trimmed_sentence.begin()+1, trimmed_sentence.end()-3, 0, ck);
return (verify == check);
}
int main()
{
std::string sentence = "$GPGGA,172814.0,3723.46587704,N,12202.26957864,W,2,6,1.2,18.893,M,-25.669,M,2.0,0031*4F";
if(nmea_checksum(sentence)) {
std::cout << "Valid!" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment