Re: Problems with hex to decimal

From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 03/04/04


Date: Thu, 4 Mar 2004 12:59:08 -0500


> I have searched the message boards and have tried the suggested method by
this board to use:
>
> = Val("&H" & hexstring) to get the decimal value....or Cstr(Val("&H" &
hexstring))
>
> This works......up to a degree. But I now have a ten digit hex number
like "03003404D2" and I want to convert it to decimal. The above equation
will only give me an answer of "3409106"......which essentially is only the
last 6 hex digits "3404D2" converted..........I can't get it to convert all
10 digits. What do I need to do to convert a 10 digit hex number to
decimal?....

Give the following function a try (it will handle up to 96-bit hex
numbers)... Note: Do NOT prefix &H onto your hex string

Rick - MVP

Function Hex2Dec(HexString As String) As Variant
  Dim X As Integer
  Dim BinStr As String
  If Len(HexString) <= 12 Then
    Const BinValues = "0000000100100011" & _
                      "0100010101100111" & _
                      "1000100110101011" & _
                      "1100110111101111"
    For X = 1 To Len(HexString)
      BinStr = BinStr & Mid$(BinValues, _
               4 * Val("&h" & Mid$(HexString, X, 1)) + 1, 4)
    Next
    For X = 0 To Len(BinStr) - 1
      Hex2Dec = Hex2Dec + Val(Mid(BinStr, _
                Len(BinStr) - X, 1)) * 2 ^ X
    Next
  Else
    ' Number is too big, handle error here
  End If
End Function



Relevant Pages