string to char const *variable

I decided the best way to learn more about string manipulation was to force myself to just do a project with them. Unfortunately I've now managed to get myself utterly confused.

Essentially I need to make a library call, in which I'd like to invoke the ap password with a name thats constructed from a start text (Node) + two bytes (which are stored in an array...). eg mywifi.autoConnect("pigeon", "Node7c4d");

The relevant line from the library is:

boolean WiFiManager::autoConnect(char const *apName, char const *apPassword)

Perhaps stupidly, Im currently dabbling with

String wifi_pass = "node"; // start text
for (int x = 1; x >= 0; x--) {
wifi_pass += String(esp_mac[x], HEX);
}

If I now serial print the string, i can see "node_c5fc". Obviously I cant pass this directly to the library so here starts the fun!

After tinkering with a few dead ends, Im going around in circles and just confusing myself more. Can anyone advise what the correct approach should be..... other than copious swearing, I've tried that too!

ps. yes i know they're snippets, but I prefer to isolate the problem :)

ps. yes i know they're snippets, but I prefer to isolate the problem

Part of the reason for no snippets is to save time trying your code. Instead, you're asking us to type in the boilerplate which may or may not be germane to your problem. A comment in your code can highlight where you think the issue is and we can go from there. I think that's one reason no one is responding. Provide the code...

Not really Jack. Im asking with a defined input format requirement (from the library), how would you guys recommend I do the construction....Im more than happy to be pointed at a resource to read :)

I can't seem to figure out what you want to accomplish!

'String', 'string' and string (as in zero terminated array of characters) are all different things. I know it doesn't help that the term has multiple definitions.

Don't describe your failure, yet, with what you think is relevant code.

What is it you are trying to do?

EDIT: Perhaps I do ...

Does this solve your problem?

String strName      = "login-name";
String strPassword  = "login-password";
wifi.autoConnect(strName.c_str(), strPassword.c_str());

Ahaaaa... thats what i was missing, thanks lloyd, I went and did some reading. Your initial 'tip' was spot on, to get a feel for what was implemented, I created this test code....

Im sure i can optimise it more (seems silly to assemble a string first then convert it), but for the moment, this is doing the job :)

byte wibble[2] = {15, 10}; // FA hex. In reality these are populated from the wifi MAC.

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println();
  method3();
}

void method3() { // successfully creates "node_fa"
  String wifi_pass_start = "node_";
  wifi_pass_start += String(wibble[0], HEX);
  wifi_pass_start += String(wibble[1], HEX);

  const char* wifi_pass = wifi_pass_start.c_str();
  
  Serial.print("Method3:  "); Serial.println(wifi_pass);
}

Time for more reading, text handling is a complex thing!

scrumfled: ... seems silly to assemble a string first then convert it ...

const char* szName      = "login-name";
const char* szPassword  = "login-password";
wifi.autoConnect(szName, szPassword);

I agree the String class seems out of place on a device with such limited resources as the 16-bit Arduino's.

I compiled your code to 3606 bytes using the String object. Getting rid of those Strings and using a C string instead:

byte wibble[2] = {15, 10}; // FA hex. In reality these are populated from the wifi MAC.

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println();
  method3();
}

void method3() { // successfully creates "node_fa"
  char wifi_pass_start[10] = "node_";
  char c[5];
  itoa(wibble[0], c, HEX);
  strcat(wifi_pass_start, c);
  itoa(wibble[1], c, HEX);
  strcat(wifi_pass_start, c);

  Serial.print("Method3:  "); Serial.println(wifi_pass_start);
}

void loop() {
  // put your main code here, to run repeatedly:

}

drops the code size to 2164 bytes. While the String class has a lot of power, many programs don't use all of that power, resulting in code size that's bigger than it needs to be.

As usual that made me read the c++ reference guide...where I discover another religious debate, this time about the use of itoa, which then leads to reading about sprintf (which I've never seen in arduino code).

Useful learning prompts, thanks again guys!

scrumfled: ... which then leads to reading about sprintf (which I've never seen in arduino code).

sprintf() has been used and debated here many times. (I googled it and found 6600 hits, many from this Forum.) As I've said before, it brings a lot to the table in terms of all of the things it can do, but it also eats a lot of resources while at that table. Most Arduino programs don't use much of its power, so alternative functions can do the same thing, but use less memory...just like the String class.