0
继this steps我写我函数来检查我扫描标签的值是正确的校验和,但它始终返回false。这是我的功能:如何检查RFID标签
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));
}
我在做什么错?
我已经做了二进制值的XOR手动检查我扫描的是一个有效的值。
EDITED
上面的代码不工作,因为它不具有“\ 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;
}
你确认你strtol将得到什么号码(hexPairs [I],NULL,16)?由于strtol需要一个char *,它需要一个终止0,这可能不会在两个字符之后产生你的数字。 – corvairjo
是的,很抱歉没有更新。我在Vinculum II IDE中工作,其调试器不让我看到这些值。我复制了CodeBlocks中的代码,可以修复终止字符问题。现在我遇到的问题是代码在CodeBlocks中工作,但不在Vinculum中。我试过声明“char \ * hexPairs [6]”并声明了6个char \ *变量,但仍然不起作用。 – adryledo
尝试使用字符串数组“char * hexPairs []”并将其填充到第一个循环中。 – corvairjo