1

My program is running on ESP32. I'm retrieving some data from Bluetooth, and the Bluetooth string is a C string. I need to save that data in an RTC_DATA_ATTR String, which is a C++ string.

Just doing c++string = cstring.c_str(); won't work, since it will just save a pointer to the location in the memory of the C string, and the purpose is to save the data in the RTC memory, so the ESP32 can go to deep sleep without losing the data.

All other data than RTC gets wiped on deep sleep, so there would just be a pointer, pointing to nothing.

How do I get the data copied from the C string to the C++ string?

Any help would be appreciated. A bit of extra info: this is how it is set up at the moment:

RTC_DATA_ATTR String ssid;

BLECharacteristic *cSsid = NULL;

ssid = cSsid->getValue().c_str();
  • you mixed it up. you want a C string from String? .c_str() of String returns a constant pointer to the internal C string (zero terminated char array) of String which is in heap. but to save it to RTC memory something must copy it to the RTC memory. it is not neccessary to copy it into 'C string' in memory. – Juraj Jan 1 at 18:31
  • yes, my question is: how do i copy the c string to my rtc memory – Marius Wanscher Jan 1 at 18:33
  • I agree with @Juraj . I believe you misused the terms cstring and c++ strings. As I understand your question, you have a class RTC_DATA_ATTR that allocates its content memory (string typed) from the RTC EEPROM. Now you ask how you are able to initialize that Class with content from a "cstring". (as you use .c_str() I believe this is also a c++ string) Because I don't know the library RTC_DATA_ATTR is from I can not say how to copy text into a string of that type. You should at least add the library name to the question or give us some information what that type is like. – Peter Paul Kiefer Jan 1 at 18:34
  • Your code would lead to a null pointer exception, which will be silently skipped.. – Peter Paul Kiefer Jan 1 at 18:38
  • 1
    RTC_DATA_ATTR char ssid[33]; strcpy(ssid, cSsid->getValue().c_str()); – Juraj Jan 1 at 18:52
0

simply, using strcpy, and using a char with a given length, works

RTC_DATA_ATTR char ssid["dedicated length"];

strcpy(ssid, cSsid->getValue().c_str(), "dedicated length");

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.