How to compare two string? I meet a terrible problem. I can't compare two string!

My job is simple, every 1ms use SoftwareSerial read_card(2, 3);to check if i read a id card.And don't read a card twice.Here are my code:

bool CardReader::check()
{
    String str= read_card.readString();
    if (str.length() == 0 ) return false;
    //Log("card:%s", str);
    Serial.println("card_id");
    Serial.println(str);
    Serial.println(card);
    Serial.print("cmp:"); Serial.println(str == card);
    if (str.length() == card.length())
    {
        uchar *p1 = (uchar*)str.c_str(), *p2 = (uchar*)str.c_str();
        for (int i = 0; i &lt; card.length(); i++)
            if (p1[i] != p2[i])
                break;
        return false;
    }
    card = str;
    return true;
}


Let's see output:

card_id
 Rq
 僐R 
cmp:1


The hex are:

00 01 52 71 02 
00 83 52 52 00 


Why the two string is equal?

Bugs are fixed.If string begin with '\0',== can't work and = can't work either.

bool CardReader::check()
{
    String str = read_card.readString();
    if (str.length() == 0) return false;
    if (str.length() == card.length())
    {
        uchar *p1 = (uchar*)str.c_str(), *p2 = (uchar*)card.c_str();
        for (int i=0; i &lt; card.length(); i++)
        {
            Log("%d:%d", p1[i],p2[i]);
            if (p1[i] != p2[i])
                goto find_card;
        }
        return false;
    }
find_card:
    for(int i=0;i&lt;5;i++)
        card[i]=str[i];
    return true;
}
Why the two string is equal?


        uchar *p1 = (uchar*)str.c_str(), *p2 = (uchar*)str.c_str();


Both p1 and p2 point to the same string so of course they will always compare equal.



Do you mean:

if (str == card)
   // do something