Skip to content

Instantly share code, notes, and snippets.

@nickelpro
Last active December 15, 2015 01:29
Show Gist options
  • Select an option

  • Save nickelpro/5180439 to your computer and use it in GitHub Desktop.

Select an option

Save nickelpro/5180439 to your computer and use it in GitHub Desktop.
Creates a std::string of hex from data, data being basically any primitive data type. I have no idea under what conditions this will explode, probably many
#include <iomanip>
#include <sstream>
std::string to_hex(const void *data, const unsigned int size) {
std::stringstream stream;
stream << std::uppercase << std::hex;
const unsigned char *bytes = static_cast<const unsigned char *>(data);
for (unsigned int i = 0; i<size; ++i) {
stream << std::setfill('0') << std::setw(2) << static_cast<const int>(bytes[i]) << " ";
}
return stream.str().substr(0, stream.str().size()-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment