diff --git a/esphome/core/bytebuffer.cpp b/esphome/core/bytebuffer.cpp index 93071a75ff..489b10d305 100644 --- a/esphome/core/bytebuffer.cpp +++ b/esphome/core/bytebuffer.cpp @@ -174,10 +174,10 @@ uint64_t ByteBuffer::get_uint64() { } float ByteBuffer::get_float() { assert(this->get_remaining() >= sizeof(float)); - unsigned char byteArray[sizeof(float)]; + uint8_t byteArray[sizeof(float)]; if (this->endianness_ == LITTLE) { - for (size_t i = 0; i < sizeof(float); i++) { - byteArray[i] = this->data_[this->position_++]; + for (uint8_t & bytePart : byteArray) { + i = this->data_[this->position_++]; } } else { for (size_t i = sizeof(float); i > 0; i--) { @@ -190,10 +190,10 @@ float ByteBuffer::get_float() { } double ByteBuffer::get_double() { assert(this->get_remaining() >= sizeof(double)); - unsigned char byteArray[sizeof(double)]; + uint8_t byteArray[sizeof(double)]; if (this->endianness_ == LITTLE) { - for (size_t i = 0; i < sizeof(double); i++) { - byteArray[i] = this->data_[this->position_++]; + for (uint8_t & bytePart : byteArray) { + i = this->data_[this->position_++]; } } else { for (size_t i = sizeof(double); i > 0; i--) { @@ -280,11 +280,11 @@ void ByteBuffer::put_uint64(uint64_t value) { } void ByteBuffer::put_float(float value) { assert(this->get_remaining() >= sizeof(float)); - unsigned char byteArray[sizeof(float)]; + uint8_t byteArray[sizeof(float)]; std::memcpy(byteArray, &value, sizeof(float)); if (this->endianness_ == LITTLE) { - for (size_t i = 0; i < sizeof(float); i++) { - this->data_[this->position_++] = byteArray[i]; + for (uint8_t bytePart : byteArray) { + this->data_[this->position_++] = bytePart; } } else { for (size_t i = sizeof(float); i > 0; i--) { @@ -294,11 +294,11 @@ void ByteBuffer::put_float(float value) { } void ByteBuffer::put_double(double value) { assert(this->get_remaining() >= sizeof(double)); - unsigned char byteArray[sizeof(double)]; + uint8_t byteArray[sizeof(double)]; std::memcpy(byteArray, &value, sizeof(double)); if (this->endianness_ == LITTLE) { - for (size_t i = 0; i < sizeof(double); i++) { - this->data_[this->position_++] = byteArray[i]; + for (uint8_t bytePart : byteArray) { + this->data_[this->position_++] = bytePart; } } else { for (size_t i = sizeof(double); i > 0; i--) { diff --git a/esphome/core/bytebuffer.h b/esphome/core/bytebuffer.h index 961caa6a26..b6149774c6 100644 --- a/esphome/core/bytebuffer.h +++ b/esphome/core/bytebuffer.h @@ -45,19 +45,19 @@ class ByteBuffer { */ static ByteBuffer wrap(const uint8_t *ptr, size_t len); // Convenience functions to create a ByteBuffer from a value - static ByteBuffer wrap(const uint8_t value); - static ByteBuffer wrap(const uint16_t value, Endian endianness = LITTLE); - static ByteBuffer wrap(const uint32_t value, Endian endianness = LITTLE); - static ByteBuffer wrap(const uint64_t value, Endian endianness = LITTLE); - static ByteBuffer wrap(const int8_t value) { return wrap((uint8_t) value); } - static ByteBuffer wrap(const int16_t value, Endian endianness = LITTLE) { return wrap((uint16_t) value, endianness); } - static ByteBuffer wrap(const int32_t value, Endian endianness = LITTLE) { return wrap((uint32_t) value, endianness); } - static ByteBuffer wrap(const int64_t value, Endian endianness = LITTLE) { return wrap((uint64_t) value, endianness); } - static ByteBuffer wrap(const float value, Endian endianness = LITTLE); - static ByteBuffer wrap(const double value, Endian endianness = LITTLE); - static ByteBuffer wrap(const bool value) { return wrap(value ? (uint8_t) 1 : (uint8_t) 0); } + static ByteBuffer wrap(uint8_t value); + static ByteBuffer wrap(uint16_t value, Endian endianness = LITTLE); + static ByteBuffer wrap(uint32_t value, Endian endianness = LITTLE); + static ByteBuffer wrap(uint64_t value, Endian endianness = LITTLE); + static ByteBuffer wrap(int8_t value) { return wrap((uint8_t) value); } + static ByteBuffer wrap(int16_t value, Endian endianness = LITTLE) { return wrap((uint16_t) value, endianness); } + static ByteBuffer wrap(int32_t value, Endian endianness = LITTLE) { return wrap((uint32_t) value, endianness); } + static ByteBuffer wrap(int64_t value, Endian endianness = LITTLE) { return wrap((uint64_t) value, endianness); } + static ByteBuffer wrap(float value, Endian endianness = LITTLE); + static ByteBuffer wrap(double value, Endian endianness = LITTLE); + static ByteBuffer wrap(bool value) { return wrap(value ? (uint8_t) 1 : (uint8_t) 0); } static ByteBuffer wrap(const std::string &value); - static ByteBuffer wrap(const std::initializer_list values); + static ByteBuffer wrap(std::initializer_list values); // Get one byte from the buffer, increment position by 1 uint8_t get_uint8();