提问者:小点点

如何在C++中比较两个字符数组?


我想比较两个值z1(CRC是从outword[10]outword[11]的最后两个级联构建的)和z2(也是CRC,但根据数据包编号计算)。
z1应该是e568,而且z2也是,但是当我比较这两个值时,我得到了一个差异。

你能告诉我问题出在哪里吗?

#include "Arduino.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

int inWord = 0;
int outWord[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
int index = 0;
unsigned char Data2Calc[]= { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
unsigned short CRC2Calc = 0;
unsigned char Bytes2Calc = 9;

void setup()
{
    pinMode(13, OUTPUT);
    lcd.begin(16,2);    
    lcd.backlight();    
    lcd.setCursor(0,0); 
    lcd.print("Test RFID");
    lcd.setCursor(0,1); 
    lcd.print("Zeskanuj TAG");
    Serial.begin(9600);
    Serial1.begin(9600);
    lcd.setCursor(0,0);
}

void loop()
{
    if (Serial1.available())
    {

        RFID();

    }
    else
    {
        if (index==11)
        {
            index=0;
        }
    }
}



unsigned char RFID(void)
{
    char z1 [10] = { "" };
    char z2 [10] = { "" };
    unsigned short crc1 [] =  { 0 };
    unsigned short crc2[]  = { 0 };

    //-----
    inWord = Serial1.read();
    index++;

    if (index == 1)
    {
        if (inWord == 1)
        {
            outWord[index] = inWord;
        }
        else
        {
            index=index-1;
        }

    }
    else if (index > 1)
    {
        if (index == 11)
        {
            outWord[index] = inWord;

            for (int i = 1; i <12; i++)
            {
            Serial.print(outWord[i],HEX);   // --> 1B31687DBC7FFE568
            Data2Calc[i-1] = outWord[i];
            }
            Serial.println();

            CRC16(Data2Calc, &CRC2Calc, Bytes2Calc);

            itoa(outWord[10],z1,16);
            itoa(outWord[11],z2,16);
            strcat(z1, z2);
            *crc2 = CRC2Calc;

            Serial.print("crc1=");      //
            Serial.print(z1);       // --> e568 
            Serial.println("");     //
            Serial.print("crc2=");      //
            sprintf(z2, "%x", *crc2);   // 
            Serial.print(z2);       // --> e568
            Serial.println("");     //
            Serial.print("CRC2Calc=");  //
            Serial.print(CRC2Calc,HEX); // --> E568
            Serial.println("");     //

            if (z1 == z2)
            {
                Serial.print("OK");
            }
            else
            {
                Serial.print("FAILED");
            }

            Serial.println("");

        }
        else
        {
        outWord[index] = inWord;
        }
    }




    return *z1;
}



void CRC16(unsigned char * Data, unsigned short * CRC, unsigned char Bytes)
{
    int i, byte;
    unsigned short C;

    *CRC = 0;
    for (byte = 1; byte <= Bytes; byte++, Data++)
    {
        C = ((*CRC >> 8) ^ *Data) << 8;
        for (i = 0; i < 8; i++)
        {
            if (C & 0x8000)
                C = (C << 1) ^ 0x1021;
            else
                C = C << 1;
        }
        *CRC = C ^ (*CRC << 8);
    }
}

程序的整个输出如下:

    Currently there are no serial ports registered - please use the + button to add a port to the monitor.
    Connect to serial port COM4 at 9600
    1B31687DBC7FFE568 
    crc1=e568 
    crc2=e568
    CRC2Calc=E568
    FAILED

共1个答案

匿名用户

不能通过比较两个char*字符串的指针来比较它们的内容,如if(z1==z2)。 这将(几乎)总是假的,因为字符串在两个不同的内存位置,因此它们的地址将不同。

您应该使用strcmp()函数,如果字符串相同,则返回零。 所以:

    if (strcmp(z1,z2) == 0)
    {
        Serial.print("OK");
    }
    else
    {
        Serial.print("FAILED");
    }

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(何在|c++|中|两个|字符|数组)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?