I am making a GUI for images edition and I need to display and handle RGB values from the user. Hence I use uint8_t to store those values and stringstream to get/set the value from/to a string. My problem is that uint8_t is considered as a char so the cast returns only the first character of the string.
Example: Let say I set the input string "123", my returned value will be 49 (the ASCII code of '1')
As I use templated functions to make the casts, I'd like to make as few changes to the code as possible (of course). Here is the cast function I use:
template<typename T>
T Cast(const std::string &str) {
std::stringstream stream;
T value;
stream << str;
stream >> value;
if (stream.fail()) {
Log(LOG_LEVEL::LERROR, "XMLCast failed to cast ", str, " to ", typeid(value).name());
}
return value;
}
So when I do
uint8_t myInt = Cast<uint8_t>("123");
I get 49 rather than 123, any idea ?