sscanf fails to read double hex

半世苍凉 提交于 2019-12-06 09:15:25

问题


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);

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 ... "Scale: 0x1.fc70e3p-1, 0x1.fc70e3p-1, 0x1.fc70e3p-1\n" ... char[1000]

So why can't it read it?

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!
}

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.


回答1:


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



来源:https://stackoverflow.com/questions/55930726/sscanf-fails-to-read-double-hex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!