我正在尝试将十六进制字符串转换为 double 值,其中一些值为负数。 This answer对于正双十六进制字符串工作正常,但当双为负时,我得到一个 std::out_of_range 异常。这是为什么?有办法解决吗?
我也曾尝试用 stringstream 做同样的事情,这对正十六进制字符串和负十六进制字符串都适用。所以总有那个解决方案,但对我来说 union 方法看起来更优雅一些。我想这是一个品味问题。
示例代码:
#include <string>
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
std::string str1("3fb999999999999a"); // 0.1
std::string str2("bfb999999999999a"); // -0.1
// ******************************
// UNION
// ******************************
union uint64double
{
unsigned long long i;
double d;
};
uint64double val1;
val1.i = std::stoll(str1, nullptr, 16);
uint64double val2;
val2.i = std::stoll(str2, nullptr, 16); // This throws std::out_of_range exception
const int w = 18;
std::cout << "USING UNION" << std::endl;
std::cout << std::setw(w) << "HEX" << std::setw(w) << "DOUBLE" << std::endl;
std::cout << std::setw(w) << str1 << std::setw(w) << val1.d << std::endl;
std::cout << std::setw(w) << str2 << std::setw(w) << val2.d << std::endl;
std::cout << std::endl;
// ******************************
// STRINGSTREAM
// ******************************
unsigned long long i1;
std::stringstream ss;
ss << std::hex << str1;
ss >> i1;
double d1(reinterpret_cast<double&>(i1));
unsigned long long i2;
ss.clear();
ss << std::hex << str2;
ss >> i2;
double d2(reinterpret_cast<double&>(i2));
std::cout << "USING STRINGSTREAM" << std::endl;
std::cout << std::setw(w) << "HEX" << std::setw(w) << "DOUBLE" << std::endl;
std::cout << std::setw(w) << str1 << std::setw(w) << d1 << std::endl;
std::cout << std::setw(w) << str2 << std::setw(w) << d2 << std::endl;
std::cout << std::endl;
return 0;
}
输出:
USING UNION
HEX DOUBLE
3fb999999999999a 0.1
bfb999999999999a 1.#QNAN
USING STRINGSTREAM
HEX DOUBLE
3fb999999999999a 0.1
bfb999999999999a -0.1
我正在使用 VS2012
最佳答案
当然stoll
抛出 std::out_of_range exception
输入 "bfb999999999999a"
,因为 0xbfb999999999999a > LLONG_MAX
.如果你使用 stoull
相反,一切都很好,因为 0xbfb999999999999a <= ULLONG_MAX
.
关于c++ - 将十六进制字符串转换为负 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27387727/