Fix number2*() functions

As correctly pointed out by 'Nable80': "%llx" does not create the 0x prefix
for the hex-string. In addition to that: snprintf() adds a terminating \0.
This commit is contained in:
Mathias Gumz 2015-01-03 21:12:40 +01:00
parent 6a9c14e620
commit fb7bc7380d

View file

@ -70,7 +70,7 @@ using std::transform;
namespace {
const size_t DIGITS10_ULONGLONGINT = 20; // ULLONG_MAX = 18446744073709551615
const size_t DIGITS16_ULONGLONGINT = 18; // ULLONG_MAX = 0xffffffffffffffff
const size_t DIGITS16_ULONGLONGINT = 16; // ULLONG_MAX = ffffffffffffffff
template <typename T>
int extractBigNumber(const char* in, T (*extractFunc)(const char*, char**, int), T& out) {
@ -173,15 +173,15 @@ int extractNumber(const std::string& in, unsigned long long& out) {
std::string number2String(long long num) {
char s[DIGITS10_ULONGLONGINT];
snprintf(s, sizeof(s), "%lld", num);
return std::string(s);
char s[DIGITS10_ULONGLONGINT+1];
int n = snprintf(s, sizeof(s), "%lld", num);
return std::string(s, n);
}
std::string number2HexString(long long num) {
char s[DIGITS16_ULONGLONGINT];
snprintf(s, sizeof(s), "%llx", num);
return std::string(s);
char s[DIGITS16_ULONGLONGINT+1];
int n = snprintf(s, sizeof(s), "%llx", num);
return std::string(s, n);
}