Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility method for SCPI protocol debugging. #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
Expand Up @@ -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