2

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

const char charArray[] PROGMEM = "Some text";
asked Aug 14, 2018 at 17:17
5
  • If you mean a C++ style string, I think you just need to do this - stackoverflow.com/questions/8960087/… 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. 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 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() {
}
answered Aug 14, 2018 at 17:35
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 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

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.