summaryrefslogtreecommitdiffstats
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--source/StringUtils.cpp58
-rw-r--r--source/StringUtils.h2
2 files changed, 59 insertions, 1 deletions
diff --git a/source/StringUtils.cpp b/source/StringUtils.cpp
index 2ec68550b..b651cc6b9 100644
--- a/source/StringUtils.cpp
+++ b/source/StringUtils.cpp
@@ -464,4 +464,60 @@ AString & UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length, AString & a
} while (tmpBytesToRead > 0);
}
- --------------------------------------------------------------------- */ \ No newline at end of file
+ --------------------------------------------------------------------- */
+
+
+
+
+
+/*
+format binary data this way:
+00001234: 31 32 33 34 35 36 37 38 39 30 61 62 63 64 65 66 1234567890abcdef
+*/
+#define HEX(x) ((x) > 9 ? (x) + 'A' - 10 : (x) + '0')
+
+AString & CreateHexDump(AString & a_Out, const void * a_Data, int a_Size, int a_LineLength)
+{
+ ASSERT(a_LineLength <= 120); // Due to using a fixed size line buffer; increase line[]'s size to lift this max
+ char line[512];
+ char * p;
+ char * q;
+
+ a_Out.reserve(a_Size / a_LineLength * (18 + 6 * a_LineLength));
+ for (int i = 0; i < a_Size; i += a_LineLength)
+ {
+ int k = a_Size - i;
+ if (k > a_LineLength)
+ {
+ k = a_LineLength;
+ }
+ memset(line, ' ', sizeof(line));
+ line[sprintf(line, "%08x:", i)] = 32; // Remove the terminating NULL that sprintf puts there
+ p = line + 10;
+ q = p + 2 + a_LineLength * 3 + 1;
+ for (int j = 0; j < k; j++)
+ {
+ unsigned char c = ((unsigned char *)a_Data)[i + j];
+ p[0] = HEX(c >> 4);
+ p[1] = HEX(c & 0xf);
+ if (c >= ' ')
+ {
+ q[0] = (char)c;
+ }
+ else
+ {
+ q[0] = '.';
+ }
+ p += 3;
+ q ++;
+ } // for j
+ q[0] = '\n';
+ q[1] = 0;
+ a_Out.append(line);
+ } // for i
+ return a_Out;
+}
+
+
+
+
diff --git a/source/StringUtils.h b/source/StringUtils.h
index 9dcb456c6..908c725ab 100644
--- a/source/StringUtils.h
+++ b/source/StringUtils.h
@@ -57,6 +57,8 @@ extern AString & RawBEToUTF8(short * a_RawData, int a_NumShorts, AString & a_UTF
/// Converts a UTF-8 string into a UTF-16 BE string, packing that back into AString; return a ref to a_UTF16
extern AString & UTF8ToRawBEUTF16(const char * a_UTF8, size_t a_UTF8Length, AString & a_UTF16);
+/// Creates a nicely formatted HEX dump of the given memory block. Max a_BytesPerLine is 120
+extern AString & CreateHexDump(AString & a_Out, const void * a_Data, int a_Size, int a_BytesPerLine);
// If you have any other string helper functions, declare them here