轻松的十六进制/浮点转换
我正在 c++ 和 python 程序之间进行一些输入/输出(仅浮点值) python 有一个很好的功能,可以将浮点值转换为十六进制数字并返回,如您在此链接中看到的:
http://docs.python.org/library/stdtypes.html#additional-methods- on-float
C++ 中是否有一种简单的方法可以实现类似的功能?并将 python 输出转换回 C++ double/float?这样,在两个进程之间交换数据时,我就不会遇到舍入错误的问题...
谢谢您的答案!
I am doing some input/output between a c++ and a python program (only floating point values) python has a nice feature of converting floating point values to hex-numbers and back as you can see in this link:
http://docs.python.org/library/stdtypes.html#additional-methods-on-float
Is there an easy way in C++ to to something similar? and convert the python output back to C++ double/float? This way I would not have the problem of rounding errors when exchanging data between the two processes...
thx for the answers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
从您在问题中提供的链接(Float 上的其他方法):
示例:
运行它:
输出:
From the link you provided in your question (Additional Methods on Float):
Example:
Run it:
Output:
您可以在两者之间发送原始二进制数据而不是字符串吗? Python标准库的struct包可以将原始数据解包为Python float对象。
Can you send raw binary data between the two instead of strings? The struct package of Python's standard library can unpack the raw data into a Python float object.
那里的文档说
%a
在 C 中执行此操作。The docs there say that
%a
does this in C.如果您相信这会解决您所有的问题,那您就大错特错了。十六进制表示法与原始浮点数完全相同。它只是使用不同的表示形式。
You're sorely mistaken if you believe that this will solve all your problems. The hex notation is exactly equivalent to the original float. It just uses a different representation.
C++ 没有任何用于将十六进制转换为浮点的库例程。原因之一是浮点的内部表示没有标准化(尽管许多编译器确实使用 IEEE 标准)。
我建议以 ASCII 格式存储,这样可以跨平台移植。您可以编写自己的库(或找到另一个库),它将在 IEEE 格式(十六进制)与内部表示形式之间进行转换。
C++ does not have any library routines for converting hex to floating point. One reason is that the internal representation of floating point is not standardized (although many compilers do use an IEEE standard).
I recommend storing in ASCII format, which is portable across platforms. You could write your own library (or find another) which will convert between an IEEE format (in hex) to the internal representation.
如果您使用的系统 C 库支持 C99 的平台,则只需使用
printf( )
和scanf( )
以及%a
格式说明符。要读取此类值,您还可以使用 C99strtod( )
、strtof( )
和strtold( )
函数:您可以选择将
NULL
替换为char **
以获取指向转换字符序列末尾的指针,并将0
替换为已知基数字符串表示形式(如果将基数设置为零,它将从格式推断基数;它将很好地解析 python 十六进制浮点数,并且还处理正常的十进制格式的浮点数)如果您的编译器/库供应商选择了不要关心 C99(基本上是 MSVC),在这些函数被合并到 C++ 标准之前你可能会运气不好(我相信它们在 C++0x 草案中,但我不完全确定) 。
If you're on a platform whose system C library supports C99, you can just use
printf( )
andscanf( )
with the%a
format specifier. For reading in such values, you can also use the C99strtod( )
,strtof( )
, andstrtold( )
functions:You can optionally replace
NULL
with achar **
to get back a pointer to the end of the sequence of converted characters, and0
with a known base for the string representation (if you set the base to be zero, it will infer the base from the format; it will parse the python hex floats just fine, and also handle normal decimal-formatted floating point)If your compiler/library vendor has chosen not to care about C99 (MSVC, basically), you're probably out of luck until these function are incorporated into the C++ standard (I believe that they're in the draft C++0x, but I'm not completely sure).
C++11 提供 std::hexfloat: IO 操纵器,包括 hexfloat
如果你可以使用 C++11 那么 hexfloat 非常容易使用。
C++11 offers std::hexfloat: IO manipulators including hexfloat
If you can use C++11 then hexfloat is pretty easy to use.