[fnd] Add SimpleTextOutput::arrayToString()

This commit is contained in:
jakcron 2018-08-14 15:23:02 +08:00
parent 70cea402e5
commit e7416b1c16
2 changed files with 23 additions and 19 deletions

View file

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <string>
#include <fnd/types.h> #include <fnd/types.h>
namespace fnd namespace fnd
@ -10,6 +11,7 @@ namespace fnd
static void hxdStyleDump(const byte_t* data, size_t len); 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, size_t row_len, size_t indent_len);
static void hexDump(const byte_t* data, size_t 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: private:
static const size_t kDefaultRowLen = 0x10; static const size_t kDefaultRowLen = 0x10;
static const size_t kDefaultByteGroupingSize = 1; static const size_t kDefaultByteGroupingSize = 1;

View file

@ -1,5 +1,8 @@
#include <fnd/SimpleTextOutput.h> #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio> #include <cstdio>
#include <fnd/SimpleTextOutput.h>
void fnd::SimpleTextOutput::hxdStyleDump(const byte_t* data, size_t len, size_t row_len, size_t byte_grouping_size) 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) 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++) for (size_t j = 0; j < indent_len; j++)
{ std::cout << " ";
putchar(' '); std::cout << arrayToString(data+i, _MIN(len-i, row_len), true, "") << std::endl;
}
}
printf("%02X", data[i]);
if ((i+1) >= len)
{
putchar('\n');
}
} }
} }
void fnd::SimpleTextOutput::hexDump(const byte_t* data, size_t len) 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++) 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();
} }