diff --git a/cores/esp32/WString.cpp b/cores/esp32/WString.cpp index b78cf2eed90..ded2a3c1e78 100644 --- a/cores/esp32/WString.cpp +++ b/cores/esp32/WString.cpp @@ -112,16 +112,28 @@ String::String(unsigned long value, unsigned char base) { *this = buf; } -String::String(float value, unsigned char decimalPlaces) { +String::String(float value, unsigned int decimalPlaces) { init(); - char buf[33]; - *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); + char *buf = (char*)malloc(decimalPlaces + 42); + if (buf) { + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); + free(buf); + } else { + *this = "nan"; + log_e("No enought memory for the operation."); + } } -String::String(double value, unsigned char decimalPlaces) { +String::String(double value, unsigned int decimalPlaces) { init(); - char buf[33]; - *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); + char *buf = (char*)malloc(decimalPlaces + 312); + if (buf) { + *this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf); + free(buf); + } else { + *this = "nan"; + log_e("No enought memory for the operation."); + } } String::~String() { diff --git a/cores/esp32/WString.h b/cores/esp32/WString.h index 958b01792e8..3190a247dda 100644 --- a/cores/esp32/WString.h +++ b/cores/esp32/WString.h @@ -71,8 +71,8 @@ class String { explicit String(unsigned int, unsigned char base = 10); explicit String(long, unsigned char base = 10); explicit String(unsigned long, unsigned char base = 10); - explicit String(float, unsigned char decimalPlaces = 2); - explicit String(double, unsigned char decimalPlaces = 2); + explicit String(float, unsigned int decimalPlaces = 2); + explicit String(double, unsigned int decimalPlaces = 2); ~String(void); // memory management diff --git a/cores/esp32/stdlib_noniso.c b/cores/esp32/stdlib_noniso.c index 8f24520dac3..e66edace7e3 100644 --- a/cores/esp32/stdlib_noniso.c +++ b/cores/esp32/stdlib_noniso.c @@ -88,7 +88,7 @@ char* ultoa(unsigned long value, char* result, int base) { return result; } -char * dtostrf(double number, signed char width, unsigned char prec, char *s) { +char * dtostrf(double number, signed int width, unsigned int prec, char *s) { bool negative = false; if (isnan(number)) { @@ -117,7 +117,7 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) { // Round correctly so that print(1.999, 2) prints as "2.00" // I optimized out most of the divisions double rounding = 2.0; - for (uint8_t i = 0; i < prec; ++i) + for (uint32_t i = 0; i < prec; ++i) rounding *= 10.0; rounding = 1.0 / rounding; diff --git a/cores/esp32/stdlib_noniso.h b/cores/esp32/stdlib_noniso.h index 3df2cc2a1b6..e5bb44e563a 100644 --- a/cores/esp32/stdlib_noniso.h +++ b/cores/esp32/stdlib_noniso.h @@ -39,7 +39,7 @@ char* utoa (unsigned int val, char *s, int radix); char* ultoa (unsigned long val, char *s, int radix); -char* dtostrf (double val, signed char width, unsigned char prec, char *s); +char* dtostrf (double val, signed int width, unsigned int prec, char *s); #ifdef __cplusplus } // extern "C"