2

I want to convert const char array in PROGMEM to String. How do I do this?

const char charArray[] PROGMEM = "Some text";
5
  • If you mean a C++ style string, I think you just need to do this - stackoverflow.com/questions/8960087/…
    – Tri
    Commented Aug 14, 2018 at 17:23
  • No, I want to convert this in Arduino-IDE. That is not work correctly! Commented Aug 14, 2018 at 17:26
  • I just saw the answer and I had no idea that was even possible. I think that is your answer.
    – Tri
    Commented Aug 14, 2018 at 17:38
  • Unfortunately that is not work. Thank you, however. Commented Aug 14, 2018 at 17:54
  • 1
    what does this mean? that is not work .... that statement provides no useful information .... describing the actual error would help
    – jsotola
    Commented Aug 14, 2018 at 19:51

1 Answer 1

5

you can use a cast to __FlashStringHelper to invoke the right constructor of the String class. It is the constructor which copies the char array from PROGMEM. __FlashStringHelper is intended as return type for the F() macro.

const char charArray[] PROGMEM = "Some text";

void setup() {

  Serial.begin(115200);

  String s((const __FlashStringHelper*) charArray);

  Serial.println(s);
}

void loop() {
}
4
  • That is right, but how do this in loop or another functions? Commented Aug 14, 2018 at 17:51
  • PROGMEM is always global. and setup() is a function like every other so same way
    – Juraj
    Commented Aug 14, 2018 at 17:54
  • Absolutely, but when I write this code in loop, the compiler will generate this error: conflicting declaration 'String s' Commented Aug 14, 2018 at 17:58
  • Thanks so much. The compiler error was due to another part of my sketch. Commented Aug 14, 2018 at 18:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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