Following this steps I wrote my function to check that the value of the tags I scan is correct, but it always returns false. Here is my function:
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));
}
What am i doing wrong?
I've done the xor of the binary values manually to check that what I scan is a valid value.
EDITED
The code above didn't work because it didn't have '\0' at the end of each pair. Solved this, my code works in CodeBlocks but in Vinculum II IDE, that is where I need it to work, always returns false. Here's the list of versions I've tried:
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);
}
I call the function this way:
if(!checksum("6D003D302040"))
{
return;
}
SOLVED
I could finally make it work changing the return. This is the final function:
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);
}
It seems that Vinculum didn't like "return (totXor == strtol(hexPairs[5], NULL, 16));".
It didn't like the matrix "char hexPairs[6][3];" either.