C 如何检查RFID标签的校验和

C 如何检查RFID标签的校验和,c,hex,checksum,rfid,bitwise-xor,C,Hex,Checksum,Rfid,Bitwise Xor,接下来,我编写了我的函数来检查我扫描的标记的值是否正确,但它总是返回false。以下是我的功能: int checksum(const char* string) { int i; char hexPairs[6][2]; long totXor; // load matrix for(i=0; i<6; i++) { hexPairs[i][0] = string[i*2]; hexPairs[i][1]

接下来,我编写了我的函数来检查我扫描的标记的值是否正确,但它总是返回false。以下是我的功能:

int checksum(const char* string)
{
    int i;
    char hexPairs[6][2];
    long totXor;

    // load matrix
    for(i=0; i<6; i++)
    {
        hexPairs[i][0] = string[i*2];
        hexPairs[i][1] = string[i*2+1];
    }

    // perform xor
    totXor = strtol(hexPairs[0], NULL, 16);
    for(i=1; i<5; i++)
    {
        totXor = totXor^strtol(hexPairs[i], NULL, 16);
    }

    return (totXor == strtol(hexPairs[5], NULL, 16));
}
int checksum(const char* string)
{
    int i;
    char *hexPairs[6];
    long totXor = 0;

    // load matrix
    for(i=0; i<6; i++)
    {
        hexPairs[i] = malloc(3);
        memset(hexPairs[i], 0, 3);
        hexPairs[i][0] = string[i*2];
        hexPairs[i][1] = string[i*2+1];
    }

    // perform xor
    totXor = strtol(hexPairs[0], NULL, 16);
    for(i=1; i<5; i++)
    {
        totXor = totXor^strtol(hexPairs[i], NULL, 16);
    }

    return ((totXor == strtol(hexPairs[5], NULL, 16)) ? 1 : 0);
}
解决

我终于可以改变报税表了。这是最后一个功能:

int checksum(const char* string)
{
    int i;
    char hexPairs[6][2];
    long totXor;

    // load matrix
    for(i=0; i<6; i++)
    {
        hexPairs[i][0] = string[i*2];
        hexPairs[i][1] = string[i*2+1];
    }

    // perform xor
    totXor = strtol(hexPairs[0], NULL, 16);
    for(i=1; i<5; i++)
    {
        totXor = totXor^strtol(hexPairs[i], NULL, 16);
    }

    return (totXor == strtol(hexPairs[5], NULL, 16));
}
int checksum(const char* string)
{
    int i;
    char *hexPairs[6];
    long totXor = 0;

    // load matrix
    for(i=0; i<6; i++)
    {
        hexPairs[i] = malloc(3);
        memset(hexPairs[i], 0, 3);
        hexPairs[i][0] = string[i*2];
        hexPairs[i][1] = string[i*2+1];
    }

    // perform xor
    totXor = strtol(hexPairs[0], NULL, 16);
    for(i=1; i<5; i++)
    {
        totXor = totXor^strtol(hexPairs[i], NULL, 16);
    }

    return ((totXor == strtol(hexPairs[5], NULL, 16)) ? 1 : 0);
}
Vinculum似乎不喜欢return-totXor==strotlhExpairs[5],NULL,16


它不喜欢矩阵字符对[6][3];或者。

您是否验证了从strtolhexPairs[i]获得的数字,NULL,16?因为strtol需要一个字符*,所以它需要一个终止的0,它可能不在构成数字的两个字符之后。是的,很抱歉没有更新。我在VinculumIIIDE中工作,它的调试器不允许我看到这些值。我复制了代码块中的代码,可以修复终止字符的问题。现在我的问题是,代码在代码块中工作,但在Viculum中不工作。我已尝试声明char*hexPairs[6]和声明6个char*variables,但仍然不起作用。尝试使用字符串数组char*hexPairs[]并在第一个循环中填充该数组。您能否详细说明修改后的版本中哪些不起作用?