c - 如何检查 RFID 标签的校验和

标签 c hex checksum rfid bitwise-xor

已关注 this steps我编写了函数来检查我扫描的标签的值是否正确,但它总是返回 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));
}

我做错了什么?

我已手动对二进制值进行异或,以检查我扫描的值是否有效。

已编辑

上面的代码不起作用,因为它在每对末尾没有“\0”。解决了这个问题,我的代码在 CodeBlocks 中工作,但在 Vinculum II IDE 中(我需要它工作的地方)总是返回 false。以下是我尝试过的版本列表:

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

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

    // 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, chk = 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);
    }

    chk = strtol(hexPairs[5], NULL, 16);

    return (totXor == chk);
}


int checksum(const char* string)
{
    char *hex0, *hex1, *hex2, *hex3, *hex4, *hexChk;
    long totXor = 0, chk = 0;

    hex0 = malloc(3);
    hex1 = malloc(3);
    hex2 = malloc(3);
    hex3 = malloc(3);
    hex4 = malloc(3);
    hexChk = malloc(3);

    memset(hex0, 0, 3);
    memset(hex1, 0, 3);
    memset(hex2, 0, 3);
    memset(hex3, 0, 3);
    memset(hex4, 0, 3);
    memset(hexChk, 0, 3);

    hex0[0] = string[0];
    hex0[1] = string[1];

    hex1[0] = string[2];
    hex1[1] = string[3];

    hex2[0] = string[4];
    hex2[1] = string[5];

    hex3[0] = string[6];
    hex3[1] = string[7];

    hex4[0] = string[8];
    hex4[1] = string[9];

    hexChk[0] = string[10];
    hexChk[1] = string[11];

    // perform xor
    totXor = strtol(hex0, NULL, 16);
    totXor = totXor^strtol(hex1, NULL, 16);
    totXor = totXor^strtol(hex2, NULL, 16);
    totXor = totXor^strtol(hex3, NULL, 16);
    totXor = totXor^strtol(hex4, NULL, 16);

    chk = strtol(hexChk, NULL, 16);

    return (totXor == chk);
}

int checksum(const char* string)
{
    char *hex0, *hex1, *hex2, *hex3, *hex4, *hexChk;
    long totXor = 0, chk = 0;

    hex0 = malloc(3);
    hex1 = malloc(3);
    hex2 = malloc(3);
    hex3 = malloc(3);
    hex4 = malloc(3);
    hexChk = malloc(3);

    memset(hex0, 0, 3);
    memset(hex1, 0, 3);
    memset(hex2, 0, 3);
    memset(hex3, 0, 3);
    memset(hex4, 0, 3);
    memset(hexChk, 0, 3);

    sprintf(hex0, "%c%c", string[0], string[1]);
    sprintf(hex1, "%c%c", string[2], string[3]);
    sprintf(hex2, "%c%c", string[4], string[5]);
    sprintf(hex3, "%c%c", string[6], string[7]);
    sprintf(hex4, "%c%c", string[8], string[9]);

    sprintf(hexChk, "%c%c", string[10], string[11]);

    // perform xor
    totXor = strtol(hex0, NULL, 16);
    totXor = totXor^strtol(hex1, NULL, 16);
    totXor = totXor^strtol(hex2, NULL, 16);
    totXor = totXor^strtol(hex3, NULL, 16);
    totXor = totXor^strtol(hex4, NULL, 16);

    chk = strtol(hexChk, NULL, 16);

    return (totXor == chk);
}

我这样调用该函数:

if(!checksum("6D003D302040"))
{
    return;
}

最佳答案

已解决

我终于可以改变返回了。这是最终的功能:

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 == strtol(hexPairs[5], NULL, 16));”。

它不喜欢矩阵“char hexPairs[6][3];”要么。

关于c - 如何检查 RFID 标签的校验和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24757371/

相关文章:

c - 模运算的结果为负

python - 如何在 Python 3.2 或更高版本中使用 'hex' 编码?

c - C语言中如何将十六进制值转换为二进制值

java - php 中的 AES/CBC/PKCS5Padding java 等价物是什么?

c# - 如何验证 UPC 或 EAN 代码?

c - 在 C 中实现双堆栈以在命令行上计算数学表达式。(段错误)

复制和释放 malloc 指针

c - 当我的文本文件包含超过限制的字符时接收信号 SIGTRAP

python - 如何在Python中将十六进制序列转换为CRC 16

python - 将数组归一化为总和为 1.0 的两种方法