Skip to content

Instantly share code, notes, and snippets.

@esutton
Last active July 20, 2022 20:18
Show Gist options
  • Save esutton/e3dc0118f6033814da94fe5d547853e5 to your computer and use it in GitHub Desktop.
Save esutton/e3dc0118f6033814da94fe5d547853e5 to your computer and use it in GitHub Desktop.
Debug print bytes in hexadecimal format 16-bytes per row similar to a hex editor display
/// Debug dump bytes in hexadecimal format 16-bytes per row
///
/// \param source
/// \param length
///
/// Example Output:
/// \code
/// 0000 bd 7a 32 13 08 1c 1e d9 - c9 48 48 0b 5f 23 1a f5
/// 0010 72 3d 8f 7a e6 2c 07 e4 - 6e 45 79 0f cb 18 13 6f
/// \endcode
void debugDumpHexBytes(const char* source, int length)
{
qDebug("------------------------");
int offset = 0;
int row = 0;
QString line;
while(length > offset )
{
line = QString::asprintf("%04x ", offset);
for(int i = 0; i < 16; ++i)
{
if(8 == i)
{
line.append(" - ");
}
uint8_t value = source[offset++];
line.append(QString::asprintf("%02x ", value));
if(length == offset)
{
break;
}
}
qDebug("%s", qPrintable(line));
++row;
}
qDebug("........................");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment