1

I need a function that has the basic functionality of the Print library, accepting different kinds of argument types and parsing them. However, I would like to then store the result in a string/char array. Does anybody know how I might utilize the Print library to do so?

3
  • RTFM: arduino.cc/en/Reference/StringConstructor Commented Aug 3, 2017 at 17:22
  • Sorry. I typically work in C. I didn't realize the string constructor was so robust. Thanks. Commented Aug 3, 2017 at 17:25
  • my StreamLib published in 2018 has CStringBuilder Commented Oct 8, 2021 at 18:35

1 Answer 1

1

You can get all the functionality of Print by creating your own class that inherits from it. For this, you only need to implement the size_t write(uint8_t) method that prints a single character. Here is a simple class that inherits from both Print and String, i.e. you get a String you can print() and println() into:

// A String you can print() and println() into.
class PrintString : public Print, public String
{
public:
    PrintString() : String() {}

    size_t write(uint8_t c) {
        *this += (char) c;
        return 1;
    }
};

And here is how you could use it:

PrintString answer;
answer.print("The answer is ");
answer.println(42);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.