Hey falks,
why is so difficult to convert a String, to a char*
I searched the whole internet and found nothing.
I have an ESP8266. I am using a String for the SSID of the ESP. This is the original code to connect the ESP to my router:
char* ssid = "Netgear";
char* password = "redacted";
WiFi.begin(ssid,password);
I have stored the ssid in a file via SPIFFS. The file is read as a string, like this:
File f = SPIFFS.open("/ssid.txt", "r");
String SatzSSID=f.readStringUntil('\n'); // this String is the SSID
It has to be a char* in the WiFi.begin();
Therefore, I have to convert it from STring, to a char*
How can I do it in the simpliest way ?
A String has a function named c_str that does this. In your case SatzSSID.c_str() should do the trick.
1 Like
That would be "SatzSSID.c_str()", wouldn't it?
"password" is a simple C string, and has no member functions.
The whole Internet except the most obvious part, the language reference.
1 Like
That is a better example, yes. OP has not mentioned what the name of the new password variable is. I updated my reply, thanks.
1 Like
thanks, will try it later in the evening ;D
I just came home from night-shift (Germany) 
Hmm... you might want to include some more useful search terms... the Internet has quite a lot of useful stuff.
https://www.google.com/search?q=convert+string+c+string+arduino
// the SSID is stored in the ip.txt as following:
// %1%Netgear%1q2w3e4r
File n = SPIFFS.open("/ssid.txt", "r");
String FileContent=n.readStringUntil('\n');
// the followig Part splits the content with a deleminiter '%'
index1 = FileContent.indexOf('%');
index2 = FileContent.indexOf('%', index1+1);
index3 = FileContent.indexOf('%', index2+1);
index4 = FileContent.indexOf('%', index3+1);
StringA = FileContent.substring(index1+1, index2);
StringB = FileContent.substring(index2+1, index3); // <-- the SSID
StringC = FileContent.substring(index3+1, index4); // <-- the passwd
char* ssid = StringB .c_str();
char* password = StringC .c_str();
WiFi.begin(StringB,StringB);
It still does not work. The Errormessage is:
invalid conversion from 'const char*' to 'char*' [-fpermissive]
Should this not be:
WiFi.begin(ssid, password);
yes, but that is only a name of variables... so does the name of a variable matter ? wait, will try.
The name does not matter, but in the snippet you shared, StringB is of type String.
yeah, at the top I used another name for this variable. Just changed it to show you the other parts of the could.
system
Closed
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.