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?
-
RTFM: arduino.cc/en/Reference/StringConstructorMajenko– Majenko08/03/2017 17:22:55Commented Aug 3, 2017 at 17:22
-
Sorry. I typically work in C. I didn't realize the string constructor was so robust. Thanks.mmiles19– mmiles1908/03/2017 17:25:21Commented Aug 3, 2017 at 17:25
-
my StreamLib published in 2018 has CStringBuilderJuraj– Juraj ♦10/08/2021 18:35:22Commented Oct 8, 2021 at 18:35
1 Answer 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);