From b6c1c3b51b44b3d7cb32e7f5f0222ecc155f8294 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sat, 3 Aug 2019 17:32:38 +0200 Subject: [PATCH] Fixed conversion for bases other than 10 --- uart.hpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/uart.hpp b/uart.hpp index b87d917..e87124d 100644 --- a/uart.hpp +++ b/uart.hpp @@ -28,7 +28,7 @@ static constexpr size_t cntDigits() } do { - num /= 10; + num /= Base; ++cnt; } while (num > 0); @@ -107,9 +107,11 @@ class Uart { } template - static inline void txNumber(const T &val) + static void txNumber(const T &val) { static_assert(util::is_integral_v, "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(); data_t buffer[numDigits]; @@ -124,7 +126,7 @@ class Uart { do { data_t lastDigit = digits % Base; - *bufEnd-- = '0' + lastDigit; + *bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : ('a' + lastDigit - 10); digits /= Base; } while (digits > 0); @@ -239,7 +241,8 @@ class Uart { Uart &operator<<(const void *val) { - txNumber(reinterpret_cast(val)); + txString(F("0x")); + txNumber(reinterpret_cast(val)); return *this; }