From 1e0af13a5246e823fc4ac2db464fe5620ac2ca8b Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 Date: Wed, 8 Sep 2021 01:48:20 +0200 Subject: [PATCH] Added toHexString() --- CMakeLists.txt | 1 + src/espstrutils.cpp | 17 +++++++++++++++++ src/espstrutils.h | 7 +++++++ 3 files changed, 25 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index c692cd4..5bdedcf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ set(dependencies esp_system # esp_http_client esp_websocket_client + libsodium tcp_transport cpputils diff --git a/src/espstrutils.cpp b/src/espstrutils.cpp index 06ef6b1..d30e67e 100644 --- a/src/espstrutils.cpp +++ b/src/espstrutils.cpp @@ -3,8 +3,11 @@ #include "sdkconfig.h" #define LOG_LOCAL_LEVEL CONFIG_ESPCPPUTILS_LOG_LOCAL_LEVEL +#include + // esp-idf includes #include +#include // 3rdparty lib includes #include @@ -58,4 +61,18 @@ std::string toString(esp_log_level_t val) } } +std::string toHexString(std::basic_string_view buf) +{ + std::string hex(buf.size() * 2 + 1, {}); + assert(hex.size() == buf.size() * 2 + 1); + + const char *ptr = sodium_bin2hex(hex.data(), hex.size(), buf.data(), buf.size()); + + hex.resize(hex.size() - 1); + assert(hex.size() == buf.size() * 2); + + ESP_LOGI(TAG, "sodium=%p end=%p", ptr, &*std::end(hex)); + return hex; +} + } // namespace espcpputils diff --git a/src/espstrutils.h b/src/espstrutils.h index 825128f..66bd539 100644 --- a/src/espstrutils.h +++ b/src/espstrutils.h @@ -2,6 +2,7 @@ // system includes #include +#include // esp-idf includes #include @@ -13,4 +14,10 @@ std::string toString(sntp_sync_mode_t val); std::string toString(sntp_sync_status_t val); std::string toString(esp_log_level_t val); +std::string toHexString(std::basic_string_view buf); +inline std::string toHexString(std::string_view str) +{ + return toHexString(std::basic_string_view{reinterpret_cast(str.data()), str.size()}); +} + } // namespace espcpputils