Problem with comparing strings

Hello everyone!
I have a little issue here I could use some advice with.
So basicall I cam sending Serial Data from my Nextion display to the Arduino.
The Arduino reads the serial data and saves it into a char array.
This works, I have checked the data by printing it via the serial monitor.
Lets say the Serial Data is 65 4 9 1 FF FF FF
with this code:

incomingByte: char 
NewSerialData: Boolean

if (incomingByte[2]==9 && incomingByte[3]==1 && NewSerialData)
{
Blubb
}

works ´just fine.

But if the data has 2 digits things act funky (or I am missing s.th.)
So, the data now is 65 4 15 1 FF FF FF
This:

incomingByte: char 
NewSerialData: Boolean

if (incomingByte[2]==15 && incomingByte[3]==1 && NewSerialData)
{
Blubb2
}

does not work. I have tried everything like =="15" , =='15' or

incomingByte: char 
NewSerialData: Boolean

if (incomingByte[2],HEX==15 && incomingByte[3],HEX==1 && NewSerialData)
{
Blubb2
}

What I am doing wrong?

Thanks for any advice!

What I am doing wrong?

You're not posting your actual code

Ok, I wanted to keep it simple because I know the array is filled correctly - but here you go :slight_smile:

index=0;

while(Serial.available() >0)
{
incomingByte[index] = Serial.read();
Serial.println(incomingByte[index],HEX)
index++;
NewSerialData=true;

}

if (incomingByte[2]==9 && incomingByte[3]==1 && NewSerialData)
{
NewSerialData=false;
do sth
}


if (incomingByte[2],HEX==15 && incomingByte[3],HEX==1 && NewSerialData)
{
NewSerialData=false;
do sth
}

if (incomingByte[2],HEX==15How likely do you think it is that fifteen will ever equal sixteen?

Not very, I agree - where do you get that 16 from?

The sixteen that you wrote there in the code I quoted.

So this:

if (incomingByte[2],HEX==15

is actually comparing if incomingByte[2] is equal to sixteen? Why so?

is actually comparing if incomingByte[2] is equal to sixteen?

No, it's actually deciding if incomingByte[2] is zero or non-zero, chucking away the result, then it is comparing sixteen to fifteen. The compiler knows that is false, so it is probably simply optimising that comparison away.

Ok, I kind of understand, kind of not.
Why is this

if (incomingByte[2]==9 && incomingByte[3]==1 && NewSerialData)
{
NewSerialData=false;
do sth
}

working, but I replace the 9 with a 15 it does not?
Could you post something that would to the trick? Maybe then I understand...

Perhaps incomingByte [2] never contains the value 15.

Who knows?

Well I have checked that by printing the contence of incomingByte to the serial monitor...
I will though fill an array manually and see if that works.
Thanks for the hint.

Ok, got it!
So I checked I converted 15,HEX to DEC, then put 21 in my IF statement and it works.
Now, I understand the arduino converted my 15[HEX] into 21[DEC] in the if statement.
Any way I can compare the value in HEX? I hope you understand, I am only a part-time-programmer...

0x15

Oh my...
thank you, AWOL. Shame on my. I added to your Karma :slight_smile:

Straight from 'The 'C' programming Language' ....

//  strcmp: return <0 if s<t, 0 if s==t, >0 if s>t 


int strcmp(char *s, char *t)
{
  for ( ; *s == *t; s++, t++)
   if (*s == '\0' )
      return 0;
  return *s - *t;

}

Allan