Char array to String

The noob is here again :slight_smile:

I actually have two questions:

  1. I’m programming the ESP8266 and the ingenious Arduino libraries (Thx to Ivan and many others) seem to use strings intensively.

Does the recommendation to NOT use strings in Arduino changes when programming the ESP8266?

  1. Using these libraries I often need to convert char arrays to strings and vice versa.
    Didn’t find an analogy for the convenient string.toCharArray so I wrote my own two versions.
void charToStringL(const char S[], String &D)
{
    byte at = 0;
    const char *p = S;
    D = "";

    while (*p++) {
      D.concat(S[at++]);
      }
}
void charToString(char S[], String &D)
{
 
 String rc(S);
 D = rc;
 
}

Both tested. The second one is twice as fast as the first one but I feel less confident with the second one as I don’t know what goes behind the scene of the assignment of a variable that goes out of scope though following tests I know its not a pointer assignment.

Any comments or suggestions are welcome.

Thanks

1 Like

Most Arduino libraries don't use Strings (uppercase S); some do support it in addition to the normal nul-terminated character arrays.

Please show me some examples of libraries that only support String (uppercase S).

The ESP8286WebServer for one.

AbleA:
2. Using these libraries I often need to convert char arrays to strings and vice versa.
Didn’t find an analogy for the convenient string.toCharArray so I wrote my own two versions.

No need. The String class already overloads the assignment operator:

char cString[] = "Hello World";

void setup() {
  String aStringObject;
  Serial.begin(115200);
  delay(2000);

  aStringObject = cString;
  Serial.println(aStringObject);
}

void loop() {
}

Thanks!

gfvalvo, Google and me missed that :-[

What about using the Strings programming the ESP8266?

As most code snippets I'm taking lessons from such as espWiFi2eeprom.ino use them freely.

I was also quite surprised to find so much use of String in the ESP8266 core. Not only in examples, but in the actual library code too. ESP8266 does have a lot more memory but still I would expect the people involved in that project to write efficient code. Of course until just the last couple of releases there were an absolutely ridiculous number of warnings in that code and they were not fixed by a member of the core development group so maybe I'm giving them too much credit.

AbleA:
gfvalvo, Google and me missed that :-[

When you have the source code, you can bypass Google and look directly in the .h and .cpp files.

What about using the Strings programming the ESP8266?

Martyn Currey has this to say about Strings and the ESP8266. He's a pretty reliable source.
http://www.martyncurrey.com/esp8266-and-the-arduino-ide-part-2-control-an-led-from-a-web-page-using-access-point-mode-ap/

Strings
A typical Arduino has very limited memory and due to how Strings (with a capital S) eat and corrupt memory using Strings is, at best, frowned upon and at worst seen as pure evil. Not so with the ESP8266. The ESP8266 has a lot more memory and the fact that web pages are a lot of text, Strings are the way to go. If you do not need to edit the html at run time, char arrays are still better though.

I'm not sure why memory corruption would't be a problem with a larger memory, but so many of the library examples and tutorials for the ESP8266 use Strings that I've adopted the practice, although I never use them in my Arduino programming.

Thank you all, your help is much appreciated

cattledog:
I'm not sure why memory corruption would't be a problem with a larger memory

It is still a problem. I just saw the first report of instability after 2 hours.

In spite of the well-documented problems (and embedded industry position), Martin Currey has decided that it is ok to use String. It's an irresponsible choice, IMO, and probably won't be fixed until enough people start complaining about instability. Even then, it's hard to point the finger at String. Sigh.