Here's a naive solution:
unsigned int str_to_hex(char const * p, char const * e) noexcept
{
unsigned int result = 0;
while (p != e)
{
result *= 16;
if ('0' <= *p && *p <= '9') { result += *p - '0'; continue; }
if ('A' <= *p && *p <= 'F') { result += *p + 10 - 'A'; continue; }
if ('a' <= *p && *p <= 'f') { result += *p + 10 - 'a'; continue; }
return -1;
}
}
std::tuple<unsigned char, unsigned char, unsigned char>
str_to_col(std::string const & s)
{
if (str.length() != 7 || s[0] == '#') { /* error */ }
auto r = str_to_hex(str.data() + 1, str.data() + 3);
auto g = str_to_hex(str.data() + 3, str.data() + 5);
auto b = str_to_hex(str.data() + 5, str.data() + 7);
if (r == -1 || g == -1 || b == -1) { /* error */ }
return {r, g, b};
}
If you validated your input elsewhere, you could abbreviate the last function to say only return { str_to_hex(...), ... };
.
Alternatively, you don't need to split the strings at all:
std::string s = "#123456";
auto n = str_to_hex(s.data() + 1, s.data() + 7);
auto r = n / 0x10000, g = (n / 0x100) % 0x100, b = n % 0x10000;
Instead of the homebrew str_to_hex
function you could also use the standard library's conversion functions, such as std::strtoul(s.substring(1), nullptr, 16)
.