stringstream is a handy class that operates on strings. You can think it a file loaded into something resembling a string, or alternatively as a sort of string that you can write to and read from like a file. It’s not exactly either of those things, but its a simplified explanation.
An example of a stringstream
#include <sstream>
stringstream ss1;
ss1 << "Hello, value is " << myInt;
ss1 << " total";
//To pass the stringstream to a string:
string s1 = ss1.str();
//To pass the stringstream to a char*:
string s1 = ss1.str().c_str();
Using variables is a stringstream
#include <sstream>
stringstream ss1;
int myInt = 54;
double myDouble = 1.234;
ss1 << "The int is " << myInt;
ss1 << ", and the double is " << myDouble; //You can add to a stringstream like this
BYTE – note you need to use (int) in front of BYTE values, they are not handled correctly without it!
Converting a stringstream into a value
#include <sstream>
stringstream ss1;
int my_value;
//float my_value;
//double my_value;
ss1 << "1.234";
ss1 >> my_value; //Will convert to int, float, double
Clear stringstream
You need to use both of these (or just create a new stringstream of course):
ss1.clear();
ss1.str(std::string());
ss1.clear(); //Resets the internal error flags (if not called the stringstream object may think it's in an error state (such as EOF), even if the underlying string has been changed).
ss1.str(std::string()); //Clear out the old string. A bit faster than using: ss1.str("")
Byte uint8_t values
You need to add (int) in front of byte or char values to get them to convert otherwise they will be dealt with as a character (char)
ss1 << (int)my_byte_value;
Length
ss1.str().length