why can't i find string using indexOf()

Hello, I don't want to create a new topic, because I've similar problem. I have to read from Serial port frame in HEX for example: 0C 00 00 00 00 06 01 03 00 ....

I need to copy 6th byte (which is 06 in this example), so I want to check the received frame using "indexOf() function). I created array with 4 bytes = 0x00: byte fourZeroes[4] = {0x00, 0x00, 0x00, 0x00};

and in loop I put that code: czteryZera = tcp_buffer.indexOf(fourZeroes);

Unfortunately, all the time I'm receiving value "-1". I might, I should have do some change i array, but I don't have any ideas. Could you help me with that?

Does the indexOf() function only work with Strings ?

UKHeliBob: Does the indexOf() function only work with Strings ?

Ok, so if I want to create variable String:

String fourZeroes= "\0\0\0\0";

it is still not working - I've checked it. Any ideas how to solve it?

String fourZeroes= "\0\0\0\0";

Why would you want four nulls in a String?

REACTIVE86:
I have to read from Serial port frame in HEX for example:
0C 00 00 00 00 06 01 03 00 …

I need to copy 6th byte (which is 06 in this example), so I want to check the received frame using "indexOf() function).
I created array with 4 bytes = 0x00:
byte fourZeroes[4] = {0x00, 0x00, 0x00, 0x00};

and in loop I put that code:
czteryZera = tcp_buffer.indexOf(fourZeroes);

Unfortunately, all the time I’m receiving value “-1”.
I might, I should have do some change i array, but I don’t have any ideas. Could you help me with that?

You can’t. Although it’s not 100% clear, the line byte fourZeroes[4] = {0x00, 0x00, 0x00, 0x00}; indicates that your working with binary data. In which case all bets of using c-style string function or String methods are off.

byte someData[] = {0x0C, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00 };
byte fourZeroes[4] = {0x00, 0x00, 0x00, 0x00};

I’m not sure of there is a standard C function, but the below can work

byte someData[] = {0x0C, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00 };
byte fourZeroes[4] = {0x00, 0x00, 0x00, 0x00};

byte *ptr = someData;
for(int cnt=0;cnt<sizeof(someData) - sizeof(fourZeroes);cnt++)
{
  if(memcmp(ptr,fourZeroes, sizeof(fourZeroes)) == 0)
  {
    Serial.print("found at position "); serial.println(cnt);
  }
  ptr++;
}

Finetune to your needs.

If you are trying to determine if “0C 00 00 00 00 06 01 03 00” contains “00 00 00 00”, why is it so difficult to see that you need:

String completeWasteOfResources = "0C 00 00 00 00 06 01 03 00";
String uselessCrap = "00 00 00 00";

  int uselessCrapPosition = completeWasteOfResurces.indexOf(uselessCrap);

PaulS: If you are trying to determine if "0C 00 00 00 00 06 01 03 00" contains "00 00 00 00", why is it so difficult to see that you need:

String completeWasteOfResources = "0C 00 00 00 00 06 01 03 00";
String uselessCrap = "00 00 00 00";

  int uselessCrapPosition = completeWasteOfResurces.indexOf(uselessCrap);

Yeah, it should be working if that frame was in strings, but it is in HEX, so I can't do this - String uselessCrap = "00 00 00 00"; because it won't help...

REACTIVE86: Yeah, it should be working if that frame was in strings, but it is in HEX, so I can't do this - String uselessCrap = "00 00 00 00"; because it won't help...

It's not hex. Hex is a text representation of numbers. So do you have a text representation of numbers or do you have numbers (binary data).

sterretje: It's not hex. Hex is a text representation of numbers. So do you have a text representation of numbers or do you have numbers (binary data).

Yeah, I know that it is not HEX. So, how should I create new String variable which consist only 4 bytes 0x00?

You don't use Strings or c-style strings when you work with binary data; see reply #4.