Skip to content

Instantly share code, notes, and snippets.

@derrickturk
Created February 25, 2014 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derrickturk/9219748 to your computer and use it in GitHub Desktop.
Save derrickturk/9219748 to your computer and use it in GitHub Desktop.
#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