Noob question: Compare byte to char.

Hi Forum,

I want to compare a byte to a character, how do I do this? in this example I want to return the message if a ‘p’ character is send.

String readCommand(unsigned long timeout)
{
  char msg[20];
  int x = 0;
  
  while(true){
    byte val = 0;
    while (digitalRead(rx) and micros() < timeout);
  
    if(micros() > timeout){
      return "TIMEOUT"; 
    }
  
    if (digitalRead(rx) == LOW) {
      delayMicroseconds(46);
      for (int offset = 0; offset < 8; offset++) {
       delayMicroseconds(92);
       val |= digitalRead(rx) << offset;
      }
    }
    msg[x] = val;
    if(val == 112){
      return msg;
    }
    
    x = x+1;
  }
}

Thank You!

It really helps if you submit all of your code so we can compile it ourselves.

convert them to the same type first.

I have more than 200 lines of code, and it compiles just fine. The problem is that I can't compare a byte to a int or char. I have tried to cast to int or char. But nothing is working. How would you convert a byte to an int for example?

Here is an example:

byte a;       (a byte from a serial connection)
int b = 112;

c = (int) a

if(a==b)
{
Serial.print("yey");
}

This does not print "yey". Why is that?

Thanks.

Because 112 is the ASCII code for 'p' 'a' is 97.

You can cast the variables in the if statement:-

if(  (char)a == b  ) {

This line does not do anything because you never do anything with c

c = (int) a