summaryrefslogtreecommitdiff
path: root/Source/Core/Common/StringUtil.cpp
diff options
context:
space:
mode:
authorPierre Bourdon <delroth@gmail.com>2016-06-17 02:08:12 +0200
committerPierre Bourdon <delroth@gmail.com>2016-06-18 16:37:09 +0200
commitfe51de23f13440fae1e289e93a0eb509cad094b5 (patch)
tree31fe82c13331b5b0c327e5eec7d0b2819de7e873 /Source/Core/Common/StringUtil.cpp
parentfd5d10005e02b304fd87cfbeaff3a6a4b4437895 (diff)
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
Diffstat (limited to 'Source/Core/Common/StringUtil.cpp')
-rw-r--r--Source/Core/Common/StringUtil.cpp33
1 files changed, 33 insertions, 0 deletions
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<char>(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)
{