Created
February 25, 2014 22:54
-
-
Save derrickturk/9219748 to your computer and use it in GitHub Desktop.
This file contains 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 <ios> | |
#include <iomanip> | |
#include <cstring> | |
#include <cstdlib> | |
int main(int argc, char **argv) | |
{ | |
if (argc != 2) { | |
std::cerr << "Usage: " << (argc ? argv[0] : "double") << " <number>\n"; | |
return 0; | |
} | |
char *end = nullptr; | |
double d = std::strtod(argv[1], &end); | |
if (d == 0.0 && end == argv[1]) { | |
std::cerr << "Invalid number: " << argv[1] << '\n'; | |
return 0; | |
} | |
auto *buf = reinterpret_cast<unsigned char*>(&d); | |
std::cout << std::hex << std::setfill('0'); | |
for (std::size_t i = 0; i < sizeof(double); ++i) { | |
std::cout << std::setw(2) << static_cast<unsigned>(buf[i]) << ' '; | |
} | |
std::cout << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment