Converting char array to string

 char buff[1000];
  int char_count = 0;
  bool received = false;
  if(Serial2.available()>0){
    while(Serial2.available()>0)
    {
     buff[char_count]=Serial2.read();
     received = true;
     char_count ++;
    }
  }


  if(received){
    received = false;
    char_count = 0;
    Serial.print(buff);
    String myString = buff;
for(int i=0;i<sizeof(myString);i++){
  if((myString[i]=='A')&&(myString[i+1]=='T')){
    Serial.print("data is correct");
  }
  }
  memset(buff, 0, sizeof buff);

while comparing its showing cannot compare String to const char*
how can i convert exact string.

please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)


And do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).


sizeof(myString) is not the string length

while (Serial2.available() > 0)
{
  buff[char_count] = Serial2.read();
  received = true;
  char_count ++;
  buff[char_count] = '\0'; //add string termination character
}

the buff array is now a C style string and you don't need to convert it to a String (uppercase S) because you can test each element of the array

for (int i = 0; i < strlen(buff) - 1; i++)
{
  if (buff[i] == 'A' && buff[i + 1] == 'T')
  {
    //do stuff
  }
}

but you need to be careful that you do not read beyond the end of the string when you use i + 1

I use sizeof operator to know the number of byte-size items (excluding the null character) of a C type string. For example:

char myString[] = "Arduino";
Serial.println(sizeof myString); //shows: 7

I use length() method to know the number of byte-size items (excluding the null character) of a String type string-object. For example:

String myString = "Arduino";
Serial.println(myString.length()); //shows: 7

Being inspired by your post #2, I am applying for the first time, the sizeof operator on a String type string-object ("Arduino"), and I have got a value 6. What does this 6 mean?

String myString = "Arduino";
Serial.println(sizeof myString); //shows: 6

It means that a String object takes 6 bytes of storage plus the space for its contents

that won't show 7.... sizeof will get the trailing null char. it's the full number of bytes in your array

you get the length of a cString with strlen() (as the underlying buffer might have unused bytes, so don't use sizeof)

You are right! My reading mistake!! Getting terribly old!!!

This is from the OutputBox of Serial Monitor.

8

coffee time :slight_smile:

1 Like

hi UKHeliBob, Thank you for you reply,
I was used your code and printed string length separately its showing 0 and my stuff also not printed.

Please post your full sketch as it is now

Please post the full sketch as it is now

when I see (poorly indented) code like this to deal with an asynchronous input, I know it will fail at one point... the MCU will empty the Serial buffer faster than data is coming in and you can't be sure you've received the full message... (esp. as you set received to true after the first byte)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.