mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[helpers] Add format_hex_prefixed_to for "0x" prefixed hex formatting (#13115)
This commit is contained in:
@@ -6,8 +6,10 @@ namespace one_wire {
|
|||||||
static const char *const TAG = "one_wire";
|
static const char *const TAG = "one_wire";
|
||||||
|
|
||||||
const std::string &OneWireDevice::get_address_name() {
|
const std::string &OneWireDevice::get_address_name() {
|
||||||
if (this->address_name_.empty())
|
if (this->address_name_.empty()) {
|
||||||
this->address_name_ = std::string("0x") + format_hex(this->address_);
|
char hex_buf[19]; // "0x" + 16 hex chars + null
|
||||||
|
this->address_name_ = format_hex_prefixed_to(hex_buf, this->address_);
|
||||||
|
}
|
||||||
return this->address_name_;
|
return this->address_name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,9 @@ void SmlTextSensor::publish_val(const ObisInfo &obis_info) {
|
|||||||
case SML_HEX: {
|
case SML_HEX: {
|
||||||
// Buffer for "0x" + up to 32 bytes as hex + null
|
// Buffer for "0x" + up to 32 bytes as hex + null
|
||||||
char buf[67];
|
char buf[67];
|
||||||
buf[0] = '0';
|
// Max 32 bytes of data fit in buffer ((67-3)/2)
|
||||||
buf[1] = 'x';
|
|
||||||
// Max 32 bytes of data fit in remaining buffer ((65-1)/2)
|
|
||||||
size_t hex_bytes = std::min(obis_info.value.size(), size_t(32));
|
size_t hex_bytes = std::min(obis_info.value.size(), size_t(32));
|
||||||
format_hex_to(buf + 2, sizeof(buf) - 2, obis_info.value.begin(), hex_bytes);
|
format_hex_prefixed_to(buf, obis_info.value.begin(), hex_bytes);
|
||||||
publish_state(buf, 2 + hex_bytes * 2);
|
publish_state(buf, 2 + hex_bytes * 2);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -759,6 +759,29 @@ inline char *format_hex_to(char (&buffer)[N], T val) {
|
|||||||
/// Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1
|
/// Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1
|
||||||
constexpr size_t format_hex_size(size_t byte_count) { return byte_count * 2 + 1; }
|
constexpr size_t format_hex_size(size_t byte_count) { return byte_count * 2 + 1; }
|
||||||
|
|
||||||
|
/// Calculate buffer size needed for format_hex_prefixed_to: "0xXXXXXXXX...\0" = bytes * 2 + 3
|
||||||
|
constexpr size_t format_hex_prefixed_size(size_t byte_count) { return byte_count * 2 + 3; }
|
||||||
|
|
||||||
|
/// Format an unsigned integer as "0x" prefixed lowercase hex to buffer.
|
||||||
|
template<size_t N, typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
|
||||||
|
inline char *format_hex_prefixed_to(char (&buffer)[N], T val) {
|
||||||
|
static_assert(N >= sizeof(T) * 2 + 3, "Buffer too small for prefixed hex");
|
||||||
|
buffer[0] = '0';
|
||||||
|
buffer[1] = 'x';
|
||||||
|
val = convert_big_endian(val);
|
||||||
|
format_hex_to(buffer + 2, N - 2, reinterpret_cast<const uint8_t *>(&val), sizeof(T));
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Format byte array as "0x" prefixed lowercase hex to buffer.
|
||||||
|
template<size_t N> inline char *format_hex_prefixed_to(char (&buffer)[N], const uint8_t *data, size_t length) {
|
||||||
|
static_assert(N >= 5, "Buffer must hold at least '0x' + one hex byte + null");
|
||||||
|
buffer[0] = '0';
|
||||||
|
buffer[1] = 'x';
|
||||||
|
format_hex_to(buffer + 2, N - 2, data, length);
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
/// Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0"
|
/// Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0"
|
||||||
constexpr size_t format_hex_pretty_size(size_t byte_count) { return byte_count * 3; }
|
constexpr size_t format_hex_pretty_size(size_t byte_count) { return byte_count * 3; }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user