Skip to content

Instantly share code, notes, and snippets.

@jhurliman
Created February 29, 2020 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhurliman/a8f66fe3571b016f6ed166015771a70d to your computer and use it in GitHub Desktop.
Save jhurliman/a8f66fe3571b016f6ed166015771a70d to your computer and use it in GitHub Desktop.
C++ Print a char* out as a hex dump
static void HexArrayToStr(const char* data, size_t length, std::string& output) {
const char* NIBBLE_TO_HEX = {"0123456789ABCDEF"};
output.assign(length * 2 + 1, 0);
char* buffer = output.data();
for (size_t i = 0; i < length; i++) {
int nibble = uint8_t(data[i]) >> 4;
buffer[2 * i] = NIBBLE_TO_HEX[nibble];
nibble = uint8_t(data[i]) & 0x0F;
buffer[2 * i + 1] = NIBBLE_TO_HEX[nibble];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment