Helper function for parsing hex numbers

This commit is contained in:
Moritz Bunkus 2012-02-05 18:24:42 +01:00
parent b19d3fcab4
commit 371ca103bd
2 changed files with 25 additions and 0 deletions

View File

@ -283,3 +283,26 @@ parse_bool(std::string value) {
return false;
throw false;
}
uint64_t
from_hex(const std::string &data) {
const char *s = data.c_str();
if (*s == 0)
throw std::bad_cast();
uint64_t value = 0;
while (*s) {
unsigned int digit = isdigit(*s) ? *s - '0'
: (('a' <= *s) && ('f' >= *s)) ? *s - 'a' + 10
: (('A' <= *s) && ('F' >= *s)) ? *s - 'A' + 10
: 16;
if (16 == digit)
throw std::bad_cast();
value = (value << 4) + digit;
++s;
}
return value;
}

View File

@ -55,4 +55,6 @@ parse_double(const std::string &s,
bool parse_bool(std::string value);
uint64_t from_hex(const std::string &data);
#endif // __MTX_COMMON_STRING_PARSING_H