Skip to content

Instantly share code, notes, and snippets.

@Vladimir-Novick
Created August 17, 2023 08:03
Show Gist options
  • Save Vladimir-Novick/d89e11f670e940fc63c33470a13fa22c to your computer and use it in GitHub Desktop.
Save Vladimir-Novick/d89e11f670e940fc63c33470a13fa22c to your computer and use it in GitHub Desktop.
Convert char array to Hex String
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