Fixed conversion for bases other than 10

This commit is contained in:
BlackMark 2019-08-03 17:32:38 +02:00
parent 8d07e2d4db
commit b6c1c3b51b

View File

@ -28,7 +28,7 @@ static constexpr size_t cntDigits()
} }
do { do {
num /= 10; num /= Base;
++cnt; ++cnt;
} while (num > 0); } while (num > 0);
@ -107,9 +107,11 @@ class Uart {
} }
template <typename T, size_t Base = 10> template <typename T, size_t Base = 10>
static inline void txNumber(const T &val) static void txNumber(const T &val)
{ {
static_assert(util::is_integral_v<T>, "Only supported on integral types"); static_assert(util::is_integral_v<T>, "Only supported on integral types");
static_assert(Base >= 2, "Numbers with bases less than 2 make no sense");
static_assert(Base <= 16, "Numbers with bases higher than 16 are not supported");
constexpr size_t numDigits = detail::maxNumDigits<T, Base>(); constexpr size_t numDigits = detail::maxNumDigits<T, Base>();
data_t buffer[numDigits]; data_t buffer[numDigits];
@ -124,7 +126,7 @@ class Uart {
do { do {
data_t lastDigit = digits % Base; data_t lastDigit = digits % Base;
*bufEnd-- = '0' + lastDigit; *bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : ('a' + lastDigit - 10);
digits /= Base; digits /= Base;
} while (digits > 0); } while (digits > 0);
@ -239,7 +241,8 @@ class Uart {
Uart &operator<<(const void *val) Uart &operator<<(const void *val)
{ {
txNumber(reinterpret_cast<uint16_t>(val)); txString(F("0x"));
txNumber<uint16_t, 16>(reinterpret_cast<uint16_t>(val));
return *this; return *this;
} }