mirror of
https://gitlab.com/mbunkus/mkvtoolnix.git
synced 2025-02-26 08:22:31 +00:00
Helper function for parsing hex numbers
This commit is contained in:
parent
b19d3fcab4
commit
371ca103bd
@ -283,3 +283,26 @@ parse_bool(std::string value) {
|
|||||||
return false;
|
return false;
|
||||||
throw 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;
|
||||||
|
}
|
||||||
|
@ -55,4 +55,6 @@ parse_double(const std::string &s,
|
|||||||
|
|
||||||
bool parse_bool(std::string value);
|
bool parse_bool(std::string value);
|
||||||
|
|
||||||
|
uint64_t from_hex(const std::string &data);
|
||||||
|
|
||||||
#endif // __MTX_COMMON_STRING_PARSING_H
|
#endif // __MTX_COMMON_STRING_PARSING_H
|
||||||
|
Loading…
Reference in New Issue
Block a user