// ----------------------------------------------------------------------------
// Copyright (C) 2014
// David Freese, W1HKJ
//
// This file is part of flmsg
//
// flrig is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// flrig is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
// ----------------------------------------------------------------------------
#ifndef _CCRC16_
#define _CCRC16_
#include
#include "debug.h"
class Ccrc16 {
private:
unsigned int crcval;
char ss[5];
public:
Ccrc16() { crcval = 0xFFFF; }
~Ccrc16() {};
void reset() { crcval = 0xFFFF;}
unsigned int val() {return crcval;}
std::string sval() {
const char *smft = "%04X";
snprintf(ss,sizeof(ss), smft, crcval);
return ss;
}
void update(char c) {
crcval ^= c;
for (int i = 0; i < 8; ++i) {
if (crcval & 1)
crcval = (crcval >> 1) ^ 0xA001;
else
crcval = (crcval >> 1);
}
}
unsigned int crc16(char c) {
update(c);
return crcval;
}
unsigned int crc16(std::string s) {
reset();
for (size_t i = 0; i < s.length(); i++)
update((char)(s[i] & 0xFF)); // only use lower half of unicode
return crcval;
}
std::string scrc16(std::string s) {
crc16(s);
return sval();
}
};
#endif