From 926903905bcf5d1de0ef528718fddbf527fc19de Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Sun, 2 Sep 2012 09:04:11 +0000 Subject: Added a CreateHexDump() function for easier raw data debugging git-svn-id: http://mc-server.googlecode.com/svn/trunk@818 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/StringUtils.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++- source/StringUtils.h | 2 ++ 2 files changed, 59 insertions(+), 1 deletion(-) 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 -- cgit v1.2.3