Skip to content

Instantly share code, notes, and snippets.

@waveacme
Last active September 10, 2021 01:25
Show Gist options
  • Save waveacme/d0b5e8803675be3f10596cb4124b0d88 to your computer and use it in GitHub Desktop.
Save waveacme/d0b5e8803675be3f10596cb4124b0d88 to your computer and use it in GitHub Desktop.
c++ version of bin2hex
//http://stackoverflow.com/questions/18424101/c-small-char-to-hex-function
#include <string>
#include <iostream>
std::string bin2hex(const std::string& input)
{
std::string res;
const char hex[] = "0123456789ABCDEF";
for(auto sc : input)
{
unsigned char c = static_cast<unsigned char>(sc);
res += hex[c >> 4];
res += hex[c & 0xf];
}
return res;
}
int main()
{
std::string example = "A string";
std::cout << "bin2hex of " << example << " gives " << bin2hex(example) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment