不工作XOR校验算法C ++ [英] Not working XOR checksum algorithm in C++

查看:276
本文介绍了不工作XOR校验算法C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搞的一团糟与电报的为c_string字符数组计算一个简单的XOR校验和。

I get messed up with a simple XOR-checksum calculated on an cstring char array for a telegram.

奇怪的是:校验工作的解码。这是一个Arduino应用程序发送从一个Arduino的一份电报到另一个。

The strange thing is: the decoding of the checksum works. It is for an Arduino application sending a telegram from one Arduino to another.

这是包括*后,正确的校验要发送的电报:

This is the telegram to be sent including a correct checksum after the *:

$ GPS,52.534015,3.9,13.496394,2.5,0.053,0,44.6,6.2 * 65

$GPS,52.534015,3.9,13.496394,2.5,0.053,0,44.6,6.2*65

您看的权利:它就像一个NEMEA消息

You look right: It is like a NEMEA message.

因此​​,这里是code的校验和计算

So here is the code for the checksum calculation

MessageStr.toCharArray(MessageBuffer, MessageStr.length()); //Arduino String Class
for (int x = 1; x < (sizeof(MessageBuffer)/sizeof(MessageBuffer[0])); x++)
{
    if (MessageBuffer[x] == '*')
    {
        Serial.println();
        break;
    }
    else
    {
        MesChecksum ^= MessageBuffer[x]; //XOR the Message data...
    }
}
MessageStr += MesChecksum;
Serial.println(MessageStr);

据计算甚至大校验和。

It calculates even to big checksums.


  • 102,而不是62

  • 102 instead 62

103,而不是67

103 instead 67

我查了这个在线工具的校验:
NEMEA校验和计算

I checked the checksum with this online tool: NEMEA checksum calc

在另一边,我使用了类似code我已经用于解析NEMEA消息:

On the other side I use a similar code I already used for parsing the NEMEA message:

boolean TelegramCheckChecksum(char* Message, int MessageLength)
{
    byte checksumReceived = 0, checksum = 0;
    for (int x = 1; x < MessageLength; x++)
    {
        if (Message[x] == '*') //Checksum starts with '*'
        {
            checksumReceived = strtol(&Message[x + 1], NULL, 16); //Parsing received checksum... |strtol parsing string to integer
            break; //Exit for and continue with next if
        }
        else
        {
            checksum ^= Message[x]; //XOR the received data...
        }
    }
    if (checksum == checksumReceived)
    {
        return true;
    }
    else
    {
        return false;
    }
}

我试过真是应有尽有。即使用手按位检查。没有任何成功。

I tried really everything. Even checking by hand bitwise. Without any success.

推荐答案

您code查找计算校验正确,但是你是不是转换的数量将它添加到邮件时为十六进制。
请注意,在十进制102是十六进制62

Your code looks correct for calculating the checksum, however you are not converting the number to hex when adding it to the message. Note that 102 in decimal is 62 in hex.

要使用标准库中的数字转换为十六进制字符串,您可以使用字符串流

To convert a number to a hex string using the standard library, you can use a stringstream

std::stringstream stream;
stream << std::hex << MesChecksum;
std::string result( stream.str() );

使用Arduino的String类你可以做这样的事情(我觉得 - 我不能测试它):

Using the Arduino String class you can do something like this (I think - I can't test it):

MessageStr += String(MesCheckSum, HEX);  

这篇关于不工作XOR校验算法C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆