简体   繁体   中英

C++: Print representation of double in hex

Is there a simple way to manipulate std::cout so that it prints doubles in their hex representation? In other words, something equivalent to:

printf("%" PRIx64, *reinterpret_cast<uint64_t *>(&my_double));

To provide some context, I have a program which prints hundreds of floating-point results and I was wondering if there is that magical one-line hack that can print all of them in hex.

Take a look at std::hexfloat if you can use C++11

Example :

double k = 3.14;
std::cout<< std::hexfloat << k << std::endl;

prints: 0x1.91eb85p+1

You can use this

    #include <iomanip> //Include this file
    cout<<hex<<*reinterpret_cast<unsigned __int64 *>(&r);

你可以让std::cout以十六进制打印:

std::cout << std::hex << num

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM