This commit is contained in:
2026-04-29 11:23:53 -05:00
parent 766032e839
commit 9229d6c86e
6 changed files with 34 additions and 13 deletions

View File

@@ -1,6 +1,7 @@
#include "bytearray.hh"
#include <cstring>
#include <cstdio>
namespace tyche {
@@ -132,4 +133,23 @@ void ByteArray::append_bytearray(ByteArray const& bytearray)
data_.insert(data_.end(), bytearray.data().begin(), bytearray.data().end());
}
std::string ByteArray::hexdump() const
{
auto to_hex = [](uint32_t value, size_t n_chars) -> std::string {
char buf[15];
snprintf(buf, sizeof buf, (std::string("%0") + std::to_string(n_chars) + "X").c_str(), value);
return { buf };
};
std::string out;
for (size_t i = 0; i < data_.size(); ++i) {
if (i % 16 == 0)
out += to_hex(i, 4) + " | ";
out += to_hex(data_.at(i), 2) + " ";
if (i % 16 == 15)
out += "\n";
}
return out + "\n";
}
}