【发布时间】:2011-10-14 13:49:23
【问题描述】:
我有一个包含“0xAp-2”的字符串。我想将其转换为双精度值:
#include <iostream>
#include <cstdio>
int main()
{
char array[] = "0xAp-2";
double a;
sscanf(array, "%lf", &a);
std::cout << a << "\n";
}
我已经尝试过 atof 和 sscanf,但无法正常工作。如果答案很明显,我深表歉意,但我已经尝试使用“sscanf”和“floating hexadecimal”等术语在 Google 上搜索。
【问题讨论】:
-
什么是array[]字符串?是否相当于 0xA0 + (p-2) ?
-
A(十六进制 10)的 -2 次方的 C 表示法很奇怪。所以除以 4。
-
您的代码在 Ubuntu 11.04 上使用 gcc 4.5 对我来说可以正常工作。
-
@Truncheon - 你的代码对我有用,无论如何在 Ideone 上(gcc-4.3.4)。 ideone.com/XSPjp
标签: c++ floating-point hex notation scanf