【问题标题】:How to convert IEEE Single to MBF Single in .NET如何在 .NET 中将 IEEE Single 转换为 MBF Single
【发布时间】:2019-04-12 23:23:37
【问题描述】:

我需要将 .NET 中的 IEEE Single 转换为旧的 MBF Single。这里有一个类似的帖子,但它与我需要的相反。

Convert MBF Single and Double to IEEE

我可以使用 VB.NET(首选)或 C# 代码。

【问题讨论】:

  • 您应该编辑您的问题以添加您想要使用的 .NET 语言,C#、C++、VB、F# 等。

标签: c# .net vb.net ieee mbf


【解决方案1】:

我在 https://community.embarcadero.com/index.php/article/technical-articles/162-programming/14799-converting-between-microsoft-binary-and-ieee-forma 遇到了一个 Borland C++ 示例

我混淆了这个示例(我不懂 C++),并在 VB.NET 中提出了以下内容。它除了零值外都有效,所以我添加了一个零检查。我不知道 C++ 代码是否也出现零错误,或者这是我的转换。无论如何,它似乎有效。

Function MTIS(value As Single) As Byte()

    Dim msbin(3) As Byte

    If value <> 0 Then ' HACK

        Dim ieee As Byte() = BitConverter.GetBytes(value)

        Dim sign As Byte
        Dim msbin_exp As Byte

        sign = ieee(3) And CByte(&H80)
        msbin_exp = msbin_exp Or ieee(3) << 1
        msbin_exp = msbin_exp Or ieee(2) >> 7

        ' An ieee exponent of 0xfe overflows in MBF
        If msbin_exp = CByte(&HFE) Then
            Throw New OverflowException("An IEEE exponent of 0xFE overflows in MBF.")
        End If

        msbin_exp += CByte(2) ' actually, -127 + 128 + 1
        msbin(3) = msbin_exp
        msbin(2) = msbin(2) Or sign
        msbin(2) = msbin(2) Or (ieee(2) And CByte(&H7F))
        msbin(1) = ieee(1)
        msbin(0) = ieee(0)

    End If

    Return msbin

End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-20
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多