Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Spreadsheets (https://www.askmehelpdesk.com/forumdisplay.php?f=395)
-   -   Convert Hexadecimal number Larger than 10 digits (https://www.askmehelpdesk.com/showthread.php?t=782251)

  • Jan 23, 2014, 01:21 AM
    thsftp
    Convert Hexadecimal number Larger than 10 digits
    Hi,

    I'm new to Excel VBA, can anyone help me with converting large Hexadecimal Number in Excel 2007.


    I've used the following code from this forum, it converts but not absolutely accurately:

    code:

    Public Function HexToDec(Hex As String) As Double

    Dim I As Long
    Dim j As Variant
    Dim k As Long
    Dim n As Long
    Dim HexArray() As Double

    n = Len(Hex)
    k = -1
    ReDim HexArray(1 To n)
    For I = n To 1 Step -1
    j = Mid(Hex, I, 1)
    k = k + 1
    Select Case j
    Case 0 To 9
    HexArray(I) = j * 16 ^ (k)
    Case Is = "A"
    HexArray(I) = 10 * 16 ^ (k)
    Case Is = "B"
    HexArray(I) = 11 * 16 ^ (k)
    Case Is = "C"
    HexArray(I) = 12 * 16 ^ (k)
    Case Is = "D"
    HexArray(I) = 13 * 16 ^ (k)
    Case Is = "E"
    HexArray(I) = 14 * 16 ^ (k)
    Case Is = "F"
    HexArray(I) = 15 * 16 ^ (k)
    End Select
    Next I
    HexToDec = Application.WorksheetFunction.Sum(HexArray)

    End Function


    The above code gives the output as "1394546016882220" in Excel to the input Hexa Value "04F4550BF58E28", whereas the correct answer is "1394546016882216".

    Please help.
  • Jan 23, 2014, 05:03 PM
    JBeaucaire
    Not sure you're going to get any better than that. Excel's precision is a known issue.

    Numeric precision in Microsoft Excel - Wikipedia, the free encyclopedia

  • All times are GMT -7. The time now is 12:21 PM.