Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iomanip>
- #include <sstream>
- #include <iostream>
- 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);
- }
- int main() {
- int myint = 20;
- std::string myhexstring = to_hex(&myint, sizeof(myint));
- std::cout << myhexstring << std::endl;
- }
- //Output
- //14 00 00 00
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement