Last active
August 29, 2015 14:23
-
-
Save 2bbb/28f0a50d7e5c183e12f5 to your computer and use it in GitHub Desktop.
new_hex_print
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 <iostream> | |
| #include <iomanip> | |
| template <typename T, size_t Size = sizeof(T)> | |
| struct HexPrintable { | |
| static void hex_print(T v) { | |
| union { | |
| T original; | |
| uint8_t printing[Size]; | |
| } value; | |
| value.original = v; | |
| std::cout << "0x"; | |
| for(int i = Size - 1; 0 <= i; i--) std::cout << std::hex << std::setfill('0') << std::setw(2) << std::uppercase << static_cast<uint16_t>(value.printing[i]); | |
| std::cout << std::endl; | |
| } | |
| }; | |
| // for long double on OSX/Xcode/Clang (it looks like 80bit float. maybe... ) | |
| // but on other environments... i don't know... | |
| template <> | |
| struct HexPrintable<long double, 16> : HexPrintable<long double, 10> {}; | |
| template <typename T> | |
| void hex_print(T v) { | |
| HexPrintable<T>::hex_print(v); | |
| } |
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 "hex_print.h" | |
| using namespace std; | |
| int main(int argc, char *argv[]) { | |
| bool b = 0; | |
| hex_print(b); | |
| char c = 61; | |
| unsigned char uc = 61; | |
| hex_print(c); | |
| hex_print(uc); | |
| long double ld = 1.0l; | |
| float f = ld; | |
| double d = ld; | |
| hex_print(f); | |
| hex_print(d); | |
| hex_print(ld); | |
| return EXIT_SUCCESS; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment