Search code examples
c++castingstringstreamuint8t

Casting from string to uint8 and vice versa


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 ?


Solution

  • You need to read the value as an unsigned (short) int first (or uint(16|32)_t, if you like), then you can truncate it to uint8_t. Since your function is templated, you can simply provide a specialization for uint8_t to handle it differently from other types:

    template<>
    uint8_t Cast<uint8_t>(const std::string &str) {
        std::istringstream stream(str);
        uint16_t value;
        if ((!(stream >> value)) || (value > 0x00FF)) {
            Log(LOG_LEVEL::LERROR, "XMLCast failed to cast ", str, " to ", typeid(uint8_t).name());
        }
        return static_cast<uint8_t>(value);
    }