Fix: FormatArrayAsHex returns gibberish instead of a hex array

pull/532/head
Rubidium 1 year ago committed by rubidium42
parent 3991e76c96
commit b5f96808a1

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

@ -12,6 +12,7 @@
#include "../3rdparty/catch2/catch.hpp"
#include "../string_func.h"
#include <array>
/**** 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(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");
}

Loading…
Cancel
Save