diff options
author | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-09-02 11:04:11 +0200 |
---|---|---|
committer | madmaxoft@gmail.com <madmaxoft@gmail.com@0a769ca7-a7f5-676a-18bf-c427514a06d6> | 2012-09-02 11:04:11 +0200 |
commit | 926903905bcf5d1de0ef528718fddbf527fc19de (patch) | |
tree | ff76fd025eef10b19e30143db118a2d9334ff865 /source | |
parent | Critical sectino now allows debug info - is it locked? / is it locked by current thread? (diff) | |
download | cuberite-926903905bcf5d1de0ef528718fddbf527fc19de.tar cuberite-926903905bcf5d1de0ef528718fddbf527fc19de.tar.gz cuberite-926903905bcf5d1de0ef528718fddbf527fc19de.tar.bz2 cuberite-926903905bcf5d1de0ef528718fddbf527fc19de.tar.lz cuberite-926903905bcf5d1de0ef528718fddbf527fc19de.tar.xz cuberite-926903905bcf5d1de0ef528718fddbf527fc19de.tar.zst cuberite-926903905bcf5d1de0ef528718fddbf527fc19de.zip |
Diffstat (limited to 'source')
-rw-r--r-- | source/StringUtils.cpp | 58 | ||||
-rw-r--r-- | source/StringUtils.h | 2 |
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 |