diff --git a/log.cpp b/log.cpp index abc622b..3b02d13 100644 --- a/log.cpp +++ b/log.cpp @@ -441,3 +441,53 @@ void Log(Severity severity, const char *format, ...) va_end(va); } } + +/** + * @brief Convert a sequence of bytes to its textual representation ("hex dump"). + * + * @param data Pointer to the byte sequence to print. + * @param len Number of bytes to print. + * + * @return string representation of the data buffer. + * + */ +string LogHexDump(const unsigned char* data, size_t len) +{ + string result; + size_t tailPos = len - 4; + char buffer[8]; + bool printable = false; + for (size_t i = 0; i < len; i++) + { + if(i<=31 || i >= tailPos) + { + if(!printable) + { + if (i) + result.append(" "); + if (i && (i % 8) == 0) + result.append(" "); + if (i && (i % 16) == 0) + result.append(" "); + } + char curByte = data[i]; + if(curByte >= 32 && curByte <= 126) + { + // Printable char + result+=curByte; + printable = true; + } + else + { + sprintf(buffer,"%02X",(unsigned char)curByte); + result.append(buffer); + printable = false; + } + } + if(i == 31) + { + result+="... "; + } + } + return result; +} \ No newline at end of file diff --git a/log.h b/log.h index d532c42..68c408d 100644 --- a/log.h +++ b/log.h @@ -248,5 +248,7 @@ ATTR_FORMAT(2, 3) void Log(Severity severity, const char *format, ...); #undef ATTR_FORMAT #undef ATTR_NORETURN +std::string LogHexDump(const unsigned char* data, size_t len); + #endif