sscanf无法读取双十六进制 [英] sscanf fails to read double hex
问题描述
我需要在文本文件中保留具有其他ascii值的某些双精度数的确切二进制表示形式,因此我正在使用
I need to preserve the exact binary representation of some doubles in a text file with other ascii values, so I am using "%a" as suggested in this question.
fprintf (pFile, "Scale: %a, %a, %a\n", scale.x, scale.y, scale.z);
但是,当我尝试用%la"读取时,scanf返回读取的0项.
However, when I try to read it in with "%la" the scanf returns 0 items read.
double x=0, y=0, z=0;
fgets(buf, sizeof buf, pFile);
int test = sscanf (buf, "Scale: %la, %la, %la\n", &x, &y, &z);
// test is zero!
打开调试器时,我看到字符串缓冲区与我期望的完全一样.
When I open up the debugger, I see that the string buffer is exactly as I expected it.
buf ...比例尺:0x1.fc70e3p-1、0x1.fc70e3p-1、0x1.fc70e3p-1 \ n" ... 字符[1000]
buf ... "Scale: 0x1.fc70e3p-1, 0x1.fc70e3p-1, 0x1.fc70e3p-1\n" ... char[1000]
那它为什么不读呢?
根据 Nate Eldredge 的要求,这是我的MCVE版本:
As requested by Nate Eldredge, here is my MCVE version:
#include <stdio.h>
int main(int argc, char *argv[])
{
double x=0, y=0, z=0;
const char* buf = "Scale: 0x1.fc70e3p-1, 0x1.fc70e3p-1, 0x1.fc70e3p-1\n";
int test = sscanf(buf, "Scale: %la , %la , %la", &x, &y, &z);
// test is zero!
}
注意:我正在使用MS Visual Studio 2013
Note: I am using MS Visual Studio 2013
第二注:我需要将源代码和数据文件发送给第三方,后者拥有自己的编译器.因此,保存格式必须相对独立于平台.
Second Note: I need to send the source code and data files to a third party, who has his own compiler. So the save format has to be relatively platform-independent.
推荐答案
Microsoft VS2013 strtod
,sscanf
和istringstream
等,未未实现c99 c ++ 11标准,并且它们无法解析十六进制浮点数或十六进制双精度点.
Microsoft VS2013 strtod
, sscanf
, and istringstream
, etc. did not implement the c99 c++11 standard and they cannot parse hex floats or hex doubles.
-
相关的错误报告:
Related Bug Report:
https://github.com/JuliaLang/julia/issues/6349
这篇关于sscanf无法读取双十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!