Last active
December 15, 2015 01:29
-
-
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
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
| #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