gpt4 book ai didi

c# - 将 MBF Single 和 Double 转换为 IEEE

转载 作者:行者123 更新时间:2023-12-04 06:52:04 24 4
gpt4 key购买 nike

Follow-Up available: There's a follow-up with further details, see Convert MBF to IEEE.



我有一些仍在使用的遗留数据,读取二进制文件不是问题,数字格式才是问题。所有浮点数都以 MBF 格式(单精度和 double )保存。我找到了 a topic about that on the MSDN boards但那个只处理 Single 值。我也想尽可能远离 API 调用。

有没有人有 double 的解决方案?

编辑:以防万一有人需要它,这是我最终得到的 VB.NET 代码(它是 Option Strict 兼容的)(随意将其转换为 C# 并在其中进行编辑):
''' <summary>Converts a MBF Single to an IEEE Single</summary>
''' <param name="src">The MBF Single value</param>
''' <returns>The converted IEEE Single value</returns>
''' <remarks>Here can find some further information about this topic: http://en.wikipedia.org/wiki/Microsoft_Binary_Format http://support.microsoft.com/kb/140520</remarks>
Public Shared Function MTIS(ByVal src As Single) As Single
Return MTIS(BitConverter.GetBytes(src), 0)
End Function

''' <summary>Converts a MBF Single to an IEEE Single</summary>
''' <param name="src">The source array</param>
''' <param name="startIndex">The start index at which the Single starts</param>
''' <returns>The converted IEEE Single value</returns>
''' <remarks>Here can find some further information about this topic: http://en.wikipedia.org/wiki/Microsoft_Binary_Format http://support.microsoft.com/kb/140520</remarks>
Public Shared Function MTIS(ByVal src() As Byte, ByVal startIndex As Integer) As Single
Dim mbf(3) As Byte
Dim ieee(3) As Byte

Array.Copy(src, startIndex, mbf, 0, 4)

If mbf(3) <> 0 Then
Dim sign As Byte = mbf(2) And ToByte(&H80)
Dim exp As Byte = mbf(3) - ToByte(2) ' -1-128-127 '

ieee(3) = ieee(3) Or sign
ieee(3) = ieee(3) Or exp >> 1
ieee(2) = ieee(2) Or exp << 7
ieee(2) = ieee(2) Or mbf(2) And ToByte(&H7F)
ieee(1) = mbf(1)
ieee(0) = mbf(0)
End If

Return BitConverter.ToSingle(ieee, 0)
End Function


''' <summary>Converts a MBF Double to a IEEE Double</summary>
''' <param name="src">The MBF Double value</param>
''' <returns>The converted IEEE Double value</returns>
''' <remarks>Here can find some further information about this topic: http://en.wikipedia.org/wiki/Microsoft_Binary_Format http://support.microsoft.com/kb/140520</remarks>
Public Shared Function MTID(ByVal src As Double) As Double
Return MTID(BitConverter.GetBytes(src), 0)
End Function

''' <summary>Converts a MBF Double to a IEEE Double</summary>
''' <param name="src">The source array</param>
''' <param name="startIndex">The start index at which the Double starts</param>
''' <returns>The converted IEEE Double value</returns>
''' <remarks>Here can find some further information about this topic: http://en.wikipedia.org/wiki/Microsoft_Binary_Format http://support.microsoft.com/kb/140520</remarks>
Public Shared Function MTID(ByVal src() As Byte, ByVal startIndex As Integer) As Double
Dim mbf(7) As Byte
Dim ieee(7) As Byte

Array.Copy(src, startIndex, mbf, 0, 8)

If mbf(7) <> 0 Then
Dim sign As Byte = mbf(6) And ToByte(&H80)
Dim exp As Int16 = mbf(7) - 128S - 1S + 1023S

ieee(7) = ieee(7) Or sign
ieee(7) = ieee(7) Or ToByte(exp >> 4 And &HFF)
ieee(6) = ieee(6) Or ToByte(exp << 4 And &HFF)

For i As Integer = 6 To 1 Step -1
mbf(i) <<= 1
mbf(i) = mbf(i) Or mbf(i - 1) >> 7
Next
mbf(0) <<= 1

For i As Integer = 6 To 1 Step -1
ieee(i) = ieee(i) Or mbf(i) >> 4
ieee(i - 1) = ieee(i - 1) Or mbf(i) << 4
Next
ieee(0) = ieee(0) Or mbf(0) >> 4
End If

Return BitConverter.ToDouble(ieee, 0)
End Function

最佳答案

this Wiki 页面上有几个不同的代码示例的链接,用于在 C、C++ 和 Python 中执行此操作。

希望其中一种或多种语言可以相对容易地转换为适合您的某种语言。

关于c# - 将 MBF Single 和 Double 转换为 IEEE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2973913/

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