gpt4 book ai didi

c# - 将 IEEE-754 之前的 C++ float 与 C# 相互转换

转载 作者:行者123 更新时间:2023-11-30 01:31:49 25 4
gpt4 key购买 nike

在 .Net 之前,在数学协处理器之前,在 IEEE-574 之前,Microsoft 为 float 定义了一种位模式。旧版本的 C++ 编译器很乐意使用该定义。

我正在编写一个 C# 应用程序,它需要在文件中读取/写入此类 float 。如何在 2 位格式之间进行转换?我需要双向的转换方法。

此应用程序将在 PocketPC/WinCE 环境中运行。

更改文件的结构超出了该项目的范围。

是否有 C++ 编译器选项指示它使用旧的 FP 格式?那将是理想的。然后,我可以使用以 null 结尾的文本字符串在 C# 代码和 C++ 代码之间交换数据,而 C++ 方法将是 sprintf 和 atof 函数的简单包装器。

至少,我希望有人能回复旧 FP 格式的位定义,这样我就可以在必要时组合一个低级位操作算法。

谢谢。

最佳答案

我关注了 Johannes Rössel 的面包屑 Wikipedia link并找到了一个不难翻译的 Python 实现:http://groups.google.com/group/comp.lang.python/browse_thread/thread/42150ccc20a1d8d5/4aadc71be8aeddbe

这是该链接中 Bengt Richter 的格式文档:

According to an old MASM 5.0 programmer's guide, there was a Microsoft Binary format for encoding real numbers, both short (32 bits) and long (64 bits).

There were 3 parts:

  1. Biased 8-bit exponent in the highest byte (last in the little-endian view we've been using) It says the bias is 0x81 for short numbers and 0x401 for long, but I'm not sure where that lines up. I just got there by experimentation.

  2. Sign bit (0 for +, 1 for -) in upper bit of second highest byte.

  3. All except the first set bit of the mantissa in the remaining 7 bits of the second highest byte, and the rest of the bytes. And since the most signficant bit for non-zero numbers is 1, it is not represented. But if if were, it would share the same bit position where the sign is (that's why I or-ed it in there to complete the actual mantissa).

MASM also supported a 10-byte format similar to IEEE. I didn't see anything in that section on NaNs and INFs.

关于c# - 将 IEEE-754 之前的 C++ float 与 C# 相互转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2686298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com