+ -
当前位置:首页 → 问答吧 → IEEE-754数据转换

IEEE-754数据转换

时间:2011-08-16

来源:互联网

小弟菜鸟,请教各位大侠
01 01 01 01 yy1 yy2 01 01 共8个字节。用IEEE754的数据转换在vs2005 上将yy1 、 yy2 2个byte 转成浮点数(单精度)能不能别写一段源代码。

作者: yesfing   发布时间: 2011-08-16

VB.NET code

Public Class Form1


    Private Function ConvertHexToSingle(ByVal hexValue As String) As Single

        Try
            Dim iInputIndex As Integer = 0
            Dim iOutputIndex As Integer = 0
            Dim bArray(3) As Byte

            For iInputIndex = 0 To hexValue.Length - 1 Step 2
                bArray(iOutputIndex) = Byte.Parse(hexValue.Chars(iInputIndex) & hexValue.Chars(iInputIndex + 1), Globalization.NumberStyles.HexNumber)
                iOutputIndex += 1
            Next

            Array.Reverse(bArray)
            Return BitConverter.ToSingle(bArray, 0)
        Catch ex As Exception
            Throw New FormatException("The supplied hex value is either empty or in an incorrect format. Use the following format: 00000000", ex)
        End Try

    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a() As String = ("01 01 01 01 AA AA 01 01").Split
        Debug.Print(ConvertHexToSingle("0000" & a(4) & a(5))) '看看哪个是你想要的结果
        Debug.Print(ConvertHexToSingle(a(4) & a(5) & "0000"))

    End Sub
End Class


作者: chinaboyzyq   发布时间: 2011-08-16

热门下载

更多