Weird output when converting character array to a string?

So I m trying to convert a character array to a string,

char myArray[2];
String myString;

void setup()
{
  Serial.begin(115200);
  for(int i = 0; i < 2; i++)
  {
    myArray[i] = 'e';
  }
  myString = String(myArray);
  Serial.print("length of string is: ");
  Serial.print(myString.length());
  Serial.println();
  Serial.println(myString);
  delay(500000);
}

void loop()
{

}

Output is,

length of string is: 4
eeî|      <-(box character as 4th character didn't paste properly)

Where as I would have expected it to be,

length of string is: 2
ee
  myString = String(myArray);

myArray is NOT a string. Do NOT pass it to a function that expects a string.

A string is a NULL terminated array of chars. Your array is NOT null-terminated. Therefore, it is not a string.

Your statement:

char myArray[2];

reserves a 2-byte bucket in memory. For this two work for 2 characters, it must be 3 elements in size. As Paul pointed out, a C string (note lower case 's') is built from an array of chars. One rule of C strings is that they are terminated with a null characters, '\0'. For your code to build a C string you would need to make these changes:

char myArray[3];   // Enough room for 2 characters plus the null termination character
// missing code...
  for(int i = 0; i < 2; i++)
  {
    myArray[i] = 'e';
  }
   myArray[i] = '\0';      // This adds the null to your string.

The use of myString uses the C++ String class (note uppercase 'S') which is a real memory hog. While the String class is easier to use on the surface, you pay a huge price in terms of memory used with Strings. Also, you should not make direct assigned between Strings and strings.

So if I add a null to my string how do I convert it so that it is a string. Is it possible to cast it?

While your code compiles to 3834 bytes, I don't think it works the way you want it to. The code below compiles to 2100 bytes and has the additional advantage that it works.

char myArray[3];    // Leave room for null


void setup()
{
  int i;
  
  Serial.begin(115200);

  for(i = 0; i < 2; i++)
  {
    myArray[i] = 'e';
  }
  myArray[i] = '\0';                          // Add the null

  Serial.print("length of string is: ");
  Serial.print(strlen(myArray));
  Serial.println();
  Serial.println(myArray);
}

void loop()
{

}

Alternatively I think I can just scrap the String function and just initialise a string variable to the nulled character array. eg

char myArray[3];
String myString;

void setup()
{
  Serial.begin(115200);
  for(int i = 0; i < 2; i++)
  {
    myArray[i] = 'e';
  }
  myArray[2] = '\0';
  myString = myArray;
  Serial.print("length of string is: ");
  Serial.print(myString.length());
  Serial.println();
  Serial.println(myString);
  delay(500000);
}

void loop()
{

}

Thank you for the help.

Why do you insist on using the String array myString? It brings absolutely nothing to the party.

So if I add a null to my string how do I convert it so that it is a string. Is it possible to cast it?

If you add a NULL to your char array, the char array IS a string. There is no need to "convert" it.

To econjack,

well because I can use many of the string functions. eg if I wanted to read a string until the end a of a line I can use Serial.readStringUntil('\n'); There seems to be a lot of support for strings functions. like stringOne.startsWith() stringOne.endsWith(). So for large portions of text it seems easier to work with.

Also when you are reading bytes individually what ever way my brain works, I fear that there will be clashes with byte commands, eg characters LF in the middle of text causing line feed. Or certain byte commands running undesirably.

Also when you say Serial.println(aString); it seems like less hassle.

So for large portions of text it seems easier to work with.

Usually, I find students who give me the "easier to work with" argument simply don't want to invest in themselves to learn a better way. Had you done your research, you'd have found Serial.readBytesUntil() which does the same thing are your readStringUntil() function, but uses a ton less memory. If you wish to bump along the String road, that's your call. However, when you start to crash into memory limits, you'll wish you had taken the time to learn C strings in the first place.

Also when you say Serial.println(aString); it seems like less hassle.

Than what? Than this?

char *string = "Print this";
Serial.println(string);

For what stupid definition of "less hassle"?

You will get along far better in here when you finally recognize that strings and String are NOT the same thing. Case matters a great deal. You keep saying one when you mean the other.

Had you done your research

Is there a reason for that negative remark. Just to let you know there are people out there with questions. That's how people learn. Why is there an expectation that I should know everything when I m asking a question on a forum?

I was well aware of Serial.readBytesUntil(); In actual fact I previously asked a question online about reducing overhead by sending individual bytes over wifi using Serial.write instead of Serial.print. I got a response from someone saying, would you not just make things easier by using Serial.println because there is plenty of speed and memory.

If you look at the Arduino examples under strings there are lots of functions for string values that simplify things. I'm looking to use string text for my own personal reasons.

For what stupid definition of "less hassle"?

Do you think its appropriate to imply someone is stupid? There is no reason for being so negative. I m just hear asking some questions which is the purpose of a forum.

Is there a reason for that negative remark. Just to let you know there are people out there with questions.

Yes. It was made in an attempt for you to dig around for answers before you come here. If you were well aware of the readBytesUntil() function, then it was silly on your part to use the readStringUntil() as an example of how easy it is to use Strings. Most of your posts seem to illustrate pretty clearly that you don't know the difference between String and string, suggesting that your research efforts were a little lacking. I think you will find that, given evidence that you have exhausted all other venues for answers, people here will bend over backwards to help. If that evidence is lacking, the responses are more aimed at pointing you someplace to find your own answers.

I wasn't aware of the difference between String and string. I was used to using upper case String and my assumption was that it was a data type just like int, float, char and thus there was no incentive to read the reference on it. But hey guess what I v learned that its an object and not a data type which wouldn't have happened if I didn't ask this question in the first place. It only takes a little bit of consideration for people generally to be a little nicer for other peoples ignorance.

But hey guess what I v learned that its an object

No, String is a class.
An object is an instance of a class.