0

この手順に従って、スキャンしたタグの値が正しいことを確認する関数を作成しましたが、常に 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));
}

私は何を間違っていますか?

バイナリ値の xor を手動で実行して、スキャンした値が有効な値であることを確認しました。

編集済み

上記のコードは、各ペアの末尾に '\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;
}
4

1 に答える 1

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 は「return (totXor == strtol(hexPairs[5], NULL, 16));」が気に入らなかったようです。

マトリックス「char hexPairs[6][3];」が気に入らなかった また。

于 2014-07-16T13:03:20.670 に答える