I've tried all the supposed solutions for the exact error, pasted into google, that I could find on every forum including this one and no matter the alleged solution I will either get the error about unsigned int to char*, the error below, or invalid pointers.
Why is this platform so completely unfriendly in data type conversion? It's almost as if the creator/s never wanted any data types to be able convert.
Here's my code that is apparently useless:
char* DeviceName = "Device-" + ESP.getChipId();
wifiManager.autoConnect(DeviceName);
Error: invalid conversion from 'const char*' to 'char*' [-fpermissive] char* DeviceName = "Device-" + ESP.getChipId();
Then I tried to use the pointers, but I have no idea what I'm doing and I keep getting invalid pointer errors, and/or invalid conversion errors.
-
Hint: Try using sprintf(). cplusplus.com/reference/cstdio/sprintfMikael Patel– Mikael Patel2018年11月26日 01:51:34 +00:00Commented Nov 26, 2018 at 1:51
-
char buffer [50]; char DeviceName = sprintf(buffer, "Device-%d", ESP.getChipId()); wifiManager.autoConnect(DeviceName);Ethan Aaron Vandal– Ethan Aaron Vandal2018年11月26日 02:04:30 +00:00Commented Nov 26, 2018 at 2:04
-
error: invalid conversion from 'char' to 'const char*' [-fpermissive] wifiManager.autoConnect(DeviceName);Ethan Aaron Vandal– Ethan Aaron Vandal2018年11月26日 02:04:49 +00:00Commented Nov 26, 2018 at 2:04
1 Answer 1
You can use the c function sprintf(, , ...) The char array has to be big enough to hold your text plus a null to terminate the string.
#include <Arduino.h>
void setup() {
Serial.begin(115200);
delay(1000);
char out[20];
sprintf(out, "Device-%08X",ESP.getChipId()); // %08X\n formats the hex and pads with 0s.
Serial.println(out);
}
void loop() {}
Output:
Device-005D4D9D
-
Thank you! I tried this and it does output a numeric ID, but I wanted the string ID. ESP8266 Chip ID = 005D4D9D <- this should be the ID... Device-6114717 <- not the numeric conversion hereEthan Aaron Vandal– Ethan Aaron Vandal2018年11月26日 02:12:36 +00:00Commented Nov 26, 2018 at 2:12
-
you are welcome - helpful user changed the format string to output 8 hex digits instead since that is what you seem to be looking for.SEngstrom– SEngstrom2018年11月26日 02:25:33 +00:00Commented Nov 26, 2018 at 2:25
-
Learning is good. To eliminate extra characters you can also use sprintf(out, "Device-%X",ESP.getChipId());Ethan Aaron Vandal– Ethan Aaron Vandal2018年11月26日 02:52:04 +00:00Commented Nov 26, 2018 at 2:52