Created
February 29, 2020 09:01
-
-
Save jhurliman/a8f66fe3571b016f6ed166015771a70d to your computer and use it in GitHub Desktop.
C++ Print a char* out as a hex dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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