Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

vb.net - Conversion Single To Hex

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...