2014-07-15 110 views
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; 
} 
+0

你確認你strtol將得到什麼號碼(hexPairs [I],NULL,16)?由於strtol需要一個char *,它需要一個終止0,這可能不會在兩個字符之後產生你的數字。 – corvairjo

+0

是的,很抱歉沒有更新。我在Vinculum II IDE中工作,其調試器不讓我看到這些值。我複製了CodeBlocks中的代碼,可以修復終止字符問題。現在我遇到的問題是代碼在CodeBlocks中工作,但不在Vinculum中。我試過聲明「char \ * hexPairs [6]」並聲明瞭6個char \ *變量,但仍然不起作用。 – adryledo

+0

嘗試使用字符串數組「char * hexPairs []」並將其填充到第一個循環中。 – corvairjo

回答

0

解決

我終於可以讓它工作改變了回報。這是最後的功能:

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不喜歡 「回報(totXor ==與strtol(hexPairs [5],NULL,16));」。

它沒有像矩陣 「炭hexPairs [6] [3];」無論是。