Added toHexString()

This commit is contained in:
2021-09-08 01:48:20 +02:00
parent b8097c25d6
commit 1e0af13a52
3 changed files with 25 additions and 0 deletions

View File

@ -32,6 +32,7 @@ set(dependencies
esp_system
# esp_http_client
esp_websocket_client
libsodium
tcp_transport
cpputils

View File

@ -3,8 +3,11 @@
#include "sdkconfig.h"
#define LOG_LOCAL_LEVEL CONFIG_ESPCPPUTILS_LOG_LOCAL_LEVEL
#include <assert.h>
// esp-idf includes
#include <esp_log.h>
#include <sodium/utils.h>
// 3rdparty lib includes
#include <fmt/core.h>
@ -58,4 +61,18 @@ std::string toString(esp_log_level_t val)
}
}
std::string toHexString(std::basic_string_view<unsigned char> 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

View File

@ -2,6 +2,7 @@
// system includes
#include <string>
#include <string_view>
// esp-idf includes
#include <esp_sntp.h>
@ -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<unsigned char> buf);
inline std::string toHexString(std::string_view str)
{
return toHexString(std::basic_string_view<unsigned char>{reinterpret_cast<const unsigned char *>(str.data()), str.size()});
}
} // namespace espcpputils