From fe51de23f13440fae1e289e93a0eb509cad094b5 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Fri, 17 Jun 2016 02:08:12 +0200 Subject: [PATCH] StringUtil: Add a HexDump function. Generates a string like the following from a binary blob: 000000: 00 00 04 74 79 70 65 00 09 61 70 70 2d 73 74 61 ...type..app-sta 000010: 72 74 rt --- Source/Core/Common/StringUtil.cpp | 33 +++++++++++++++++++++++++++++++ Source/Core/Common/StringUtil.h | 3 +++ 2 files changed, 36 insertions(+) diff --git a/Source/Core/Common/StringUtil.cpp b/Source/Core/Common/StringUtil.cpp index 2eb68a944a..007da2685a 100644 --- a/Source/Core/Common/StringUtil.cpp +++ b/Source/Core/Common/StringUtil.cpp @@ -36,6 +36,39 @@ static locale_t GetCLocale() } #endif +std::string HexDump(const u8* data, size_t size) +{ + constexpr size_t BYTES_PER_LINE = 16; + + std::string out; + for (size_t row_start = 0; row_start < size; row_start += BYTES_PER_LINE) + { + out += StringFromFormat("%06zx: ", row_start); + for (size_t i = 0; i < BYTES_PER_LINE; ++i) + { + if (row_start + i < size) + { + out += StringFromFormat("%02hhx ", data[row_start + i]); + } + else + { + out += " "; + } + } + out += " "; + for (size_t i = 0; i < BYTES_PER_LINE; ++i) + { + if (row_start + i < size) + { + char c = static_cast(data[row_start + i]); + out += StringFromFormat("%c", isprint(c) ? c : '.'); + } + } + out += "\n"; + } + return out; +} + // faster than sscanf bool AsciiToHex(const std::string& _szValue, u32& result) { diff --git a/Source/Core/Common/StringUtil.h b/Source/Core/Common/StringUtil.h index 8d8ca4c9b2..2c91a9a252 100644 --- a/Source/Core/Common/StringUtil.h +++ b/Source/Core/Common/StringUtil.h @@ -96,6 +96,9 @@ bool TryParseVector(const std::string& str, std::vector* output, const char d return true; } +// Generates an hexdump-like representation of a binary data blob. +std::string HexDump(const u8* data, size_t size); + // TODO: kill this bool AsciiToHex(const std::string& _szValue, u32& result);