From e7416b1c16641ba4b4d1ede26bd18b9a86cc632c Mon Sep 17 00:00:00 2001 From: jakcron Date: Tue, 14 Aug 2018 15:23:02 +0800 Subject: [PATCH] [fnd] Add SimpleTextOutput::arrayToString() --- lib/libfnd/include/fnd/SimpleTextOutput.h | 2 ++ lib/libfnd/source/SimpleTextOutput.cpp | 40 ++++++++++++----------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/libfnd/include/fnd/SimpleTextOutput.h b/lib/libfnd/include/fnd/SimpleTextOutput.h index bbf8e73..ef0516f 100644 --- a/lib/libfnd/include/fnd/SimpleTextOutput.h +++ b/lib/libfnd/include/fnd/SimpleTextOutput.h @@ -1,4 +1,5 @@ #pragma once +#include #include namespace fnd @@ -10,6 +11,7 @@ namespace fnd static void hxdStyleDump(const byte_t* data, size_t len); static void hexDump(const byte_t* data, size_t len, size_t row_len, size_t indent_len); static void hexDump(const byte_t* data, size_t len); + static std::string arrayToString(const byte_t* data, size_t len, bool upper_case, const std::string& separator); private: static const size_t kDefaultRowLen = 0x10; static const size_t kDefaultByteGroupingSize = 1; diff --git a/lib/libfnd/source/SimpleTextOutput.cpp b/lib/libfnd/source/SimpleTextOutput.cpp index 7dbc17d..c7f1582 100644 --- a/lib/libfnd/source/SimpleTextOutput.cpp +++ b/lib/libfnd/source/SimpleTextOutput.cpp @@ -1,5 +1,8 @@ -#include +#include +#include +#include #include +#include void fnd::SimpleTextOutput::hxdStyleDump(const byte_t* data, size_t len, size_t row_len, size_t byte_grouping_size) { @@ -58,31 +61,30 @@ void fnd::SimpleTextOutput::hxdStyleDump(const byte_t* data, size_t len) void fnd::SimpleTextOutput::hexDump(const byte_t* data, size_t len, size_t row_len, size_t indent_len) { - for (size_t i = 0; i < len; i++) + for (size_t i = 0; i < len; i += row_len) { - if ((i % row_len) == 0) - { - if (i > 0) - putchar('\n'); - for (size_t j = 0; j < indent_len; j++) - { - putchar(' '); - } - } - printf("%02X", data[i]); - if ((i+1) >= len) - { - putchar('\n'); - } - + for (size_t j = 0; j < indent_len; j++) + std::cout << " "; + std::cout << arrayToString(data+i, _MIN(len-i, row_len), true, "") << std::endl; } } void fnd::SimpleTextOutput::hexDump(const byte_t* data, size_t len) { + std::cout << arrayToString(data, len, true, "") << std::endl; +} + +std::string fnd::SimpleTextOutput::arrayToString(const byte_t* data, size_t len, bool upper_case, const std::string& separator) +{ + std::stringstream ss; + + if (upper_case) + ss << std::uppercase; for (size_t i = 0; i < len; i++) { - printf("%02X", data[i]); + ss << std::hex << std::setw(2) << std::setfill('0') << (uint32_t)data[i]; + if (i+1 < len) + ss << separator; } - putchar('\n'); + return ss.str(); } \ No newline at end of file