Conversion Single To Hex

冷暖自知 提交于 2021-01-29 07:32:43

问题


I am trying to convert the following VB6 code to VB.NET:

Public Function SingleToHex(ByVal Tmp As Single) As String
    Dim TmpBytes(0 To 3) As Byte
    Dim TmpSng As Single
    Dim tmpStr As String
    Dim x As Long
    TmpSng = Tmp
    Call CopyMemory(ByVal VarPtr(TmpBytes(0)), ByVal VarPtr(TmpSng), 4)
    For x = 3 To 0 Step -1
        If Len(Hex(TmpBytes(x))) = 1 Then
            tmpStr = tmpStr & "0" & Hex(TmpBytes(x))
        Else
            tmpStr = tmpStr & Hex(TmpBytes(x))
        End If
    Next x
    SingleToHex = tmpStr
End Function

I tried to find a function in the "Conversions" namespace, but I did not find any.

Can anybody tell me how this can easily be done?


回答1:


Public Function SingleToHex(ByVal Tmp As Single) As String
    Dim arr = BitConverter.GetBytes(Tmp)
    Array.Reverse(arr)
    Return BitConverter.ToString(arr).Replace("-", "")
End Function


来源:https://stackoverflow.com/questions/31506928/conversion-single-to-hex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!