Skip to content

Instantly share code, notes, and snippets.

@KentaYamada
Created June 27, 2016 15:05
Show Gist options
  • Save KentaYamada/6ddaf3c02136284c08197f980d57b9f7 to your computer and use it in GitHub Desktop.
Save KentaYamada/6ddaf3c02136284c08197f980d57b9f7 to your computer and use it in GitHub Desktop.
パリティチェック?
#include <stdlib.h>
#include <iostream>
#include <string>
#include <sstream>
bool CheckFCC(std::string &buf, int start, size_t byte_count)
{
char fcc = buf[byte_count + 4];
char calc_fcc;
for (size_t i = 0; i < byte_count + 4; i++)
{
calc_fcc ^= buf[start + i];
}
std::stringstream ss;
ss << "Recieved fcc: " << (int)fcc << " Calculated fcc: " << (int)calc_fcc;
return fcc == calc_fcc ? true : false;
}
bool CheckFCC(unsigned char buf[], int start, size_t byte_count)
{
unsigned char fcc = buf[byte_count + 4];
unsigned char calc_fcc = 0x00;
for (size_t i = 0; i < byte_count + 4; i++)
{
calc_fcc ^= buf[start + i];
}
std::stringstream ss;
ss << "Recieved fcc: " << (int)fcc << " Calculated fcc: " << (int)calc_fcc;
return fcc == calc_fcc ? true : false;
}
int main(int argc, char *argv[])
{
unsigned char data[] = { 0x01, 0x40, 0x0c, 0x0d,
0x81, 0xd5, 0x40, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0xff, 0x02,
0xf0, 0x59 };
std::string s1;
for (size_t i = 0; i < sizeof(data); i++) {
s1.push_back(data[i]);
}
unsigned char data2[] = { 0x02, 0x40, 0x0c, 0x09,
0x81, 0xd4, 0x40, 0x01,
0x00, 0x00, 0x00, 0x00,
0x00, 0x53 };
std::string s2;
for (size_t i = 0; i < sizeof(data2); i++) {
s1.push_back(data2[i]);
}
bool fcc = CheckFCC(data, 0, data[3]);
if (!fcc)
{
std::cout << "fcc error." << std::endl;
}
else
{
std::cout << "ok." << std::endl;
}
fcc = CheckFCC(data2, 0, data2[3]);
if (!fcc)
{
std::cout << "fcc error." << std::endl;
}
else
{
std::cout << "ok." << std::endl;
}
fcc = CheckFCC(s1, 0, s1[3]);
if (!fcc)
{
std::cout << "fcc error." << std::endl;
}
else
{
std::cout << "ok." << std::endl;
}
fcc = CheckFCC(s2, 0, s2[3]);
if (!fcc)
{
std::cout << "fcc error." << std::endl;
}
else
{
std::cout << "ok." << std::endl;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment