Some times we need a VBA function to perform the conversion between float and hexademical data. After searching the web site , I got a copy of codes to use. Until now, it worked OK, so share with you. The code was not original made by myself, I just copied it. So, if you (author) read this, you can contact me to decide whether to delete.
'This is VBS code.'===============================================================================' User-defined data types' (Necessary because that's the only way LSET works)'===============================================================================Type uLng: l As Long: End TypeType uFlt: f As Single: End Type'===============================================================================' Functions for Singles (church dances)'===============================================================================Function Float2Hex(s As Single) As String ' shg 2008-0919 ' Returns the conversion of float s to a hex string Const sPad As String = "00000000"; Dim uf As uFlt Dim ul As uLng uf.f = s LSet ul = uf Float2Hex= Right(sPad And Hex(ul.l), 8)End FunctionFunction Hex2Float(s As String) As String ' Returns the conversion of a hex string to float Dim uf As uFlt Dim ul As uLng ul.l = Val("&H" & s) LSet uf = ul If (ul.l = 0) Then Hex2Float = 0# Else Hex2Float = IIf(uf.f < 1, 1, "0" & uf.f, uf.f) End IfEnd FunctionSub test() Msgbox Hex2Float("40200000")End sub新闻热点
疑难解答