1
0
Fork 0

Fix: FormatArrayAsHex returns gibberish instead of a hex array

pull/10728/head
Rubidium 2023-04-27 23:14:01 +02:00 committed by rubidium42
parent 3991e76c96
commit b5f96808a1
2 changed files with 13 additions and 4 deletions

View File

@ -135,14 +135,14 @@ char *stredup(const char *s, const char *last)
*/ */
std::string FormatArrayAsHex(span<const byte> data) std::string FormatArrayAsHex(span<const byte> data)
{ {
std::ostringstream ss; std::string str;
ss << std::uppercase << std::setfill('0') << std::setw(2) << std::hex; str.reserve(data.size() * 2 + 1);
for (auto b : data) { for (auto b : data) {
ss << b; fmt::format_to(std::back_inserter(str), "{:02X}", b);
} }
return ss.str(); return str;
} }
/** /**

View File

@ -12,6 +12,7 @@
#include "../3rdparty/catch2/catch.hpp" #include "../3rdparty/catch2/catch.hpp"
#include "../string_func.h" #include "../string_func.h"
#include <array>
/**** String compare/equals *****/ /**** String compare/equals *****/
@ -516,3 +517,11 @@ TEST_CASE("StrEndsWithIgnoreCase - std::string_view")
CHECK(!StrEndsWithIgnoreCase(base.substr(2, 1), base.substr(0, 1))); CHECK(!StrEndsWithIgnoreCase(base.substr(2, 1), base.substr(0, 1)));
CHECK(!StrEndsWithIgnoreCase(base.substr(0, 1), base.substr(0, 2))); CHECK(!StrEndsWithIgnoreCase(base.substr(0, 1), base.substr(0, 2)));
} }
TEST_CASE("FormatArrayAsHex")
{
CHECK(FormatArrayAsHex(std::array<byte, 0>{}) == "");
CHECK(FormatArrayAsHex(std::array<byte, 1>{0x12}) == "12");
CHECK(FormatArrayAsHex(std::array<byte, 4>{0x13, 0x38, 0x42, 0xAF}) == "133842AF");
}