Given double foo
我可以使用十六进制格式字符串分配它sscanf
像这样:
sscanf("0XD", "%lg", &foo)
但我似乎无法得到istringstream
以同样的方式行事。所有这些都只需写入 0 即可foo
:
istringstream("0XD") >> foo
istringstream("0XD") >> hex >> foo
istringstream("D") >> hex >> foo
当我读到这一点时,这一点尤其令人担忧here http://en.cppreference.com/w/cpp/locale/num_get/get认为double
istream
提取操作员应该:
检查是否char
从前面的步骤获得的内容允许出现在将被解析的输入字段中std::scanf
给定转换说明符
为什么我无法读取十六进制istream
?我写了一些测试代码here http://ideone.com/Dkud7R如果对测试有帮助的话。
您正在寻找的是hexfloat http://en.cppreference.com/w/cpp/io/manip/fixed修饰符。这hex
修饰符用于整数。
在兼容的编译器上,这将解决您的问题。
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
using namespace std;
int main() {
double foo;
sscanf("0xd", "%lg", &foo);
cout << foo << endl;
istringstream("0xd") >> hexfloat >> foo;
cout << foo << endl;
istringstream("d") >> hexfloat >> foo;
cout << foo << endl;
}
Using 视觉工作室 2015 http://webcompiler.cloudapp.net/将产生:
13
13
13
Using libc++ http://coliru.stacked-crooked.com/a/6b383f5f18d83d18将产生:
13
13
0
所以我不确定其合法性istringstream("d") >> hexfloat >> foo
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)