How can one format double as hex?
double t = 1.123;
fmt::format("{:x}", t);
It throws exception "invalid type specifier".
I would like to get string 3ff1f7ced916872b
You can use std::bit_cast
to cast double
into an appropriately sized integer and format that in hexadecimal, eg assuming IEEE754 double
:
double t = 1.123;
auto s = fmt::format("{:x}", std::bit_cast<uint64_t>(t));
// s == "3ff1f7ced916872b"
godbolt: https://godbolt.org/z/ehKTrMz7M
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.