Created
August 17, 2023 08:03
-
-
Save Vladimir-Novick/d89e11f670e940fc63c33470a13fa22c to your computer and use it in GitHub Desktop.
Convert char array to Hex String
This file contains 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
string CharArrayToHex(char* data, size_t len) | |
{ | |
constexpr char hexmap[] = { '0', '1', '2', '3', '4', '5', '6', '7', | |
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; | |
std::string s(len * 2, ' '); | |
unsigned char p; | |
for (int i = 0; i < len; ++i) { | |
p = data[i]; | |
s[2 * i] = hexmap[(p & 0xF0) >> 4]; | |
s[2 * i + 1] = hexmap[p & 0x0F]; | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment