Page 1 of 2 12 LastLast
Results 1 to 40 of 75

Thread: Convert huge Hex number to Decimal

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Convert huge Hex number to Decimal

    Dear guys
    I am trying to convert huge hex number to dec in Vb6 but I couldn't manage .
    I found one function which it could convert 23 hex number to dec but I want to convert more for example 300 or even more.
    I would like to ask you guys if you have source code for that ,please post it here .
    Thank you .

  2. #2
    Addicted Member xavierjohn22's Avatar
    Join Date
    Oct 2006
    Location
    Approx. 4921' and 3.11" asl
    Posts
    249

  3. #3
    PowerPoster Fazi's Avatar
    Join Date
    Aug 2005
    Location
    Underworld
    Posts
    2,525

    Re: Convert huge Hex number to Decimal

    When you assing the hex to a long / interger variable, it automatcally converts it. not sure about other special functions for this.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Convert huge Hex number to Decimal

    You can convert hex to decimal using the Chr$() function:

    MsgBox Chr$("&H20")

    What do you mean converting a hex value of "300 or more"? Do you mean one really long hex value or a bunch of different ones?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by DigiRev
    You can convert hex to decimal using the Chr$() function:

    MsgBox Chr$("&H20")

    What do you mean converting a hex value of "300 or more"? Do you mean one really long hex value or a bunch of different ones?
    Yes ,I mean 0ne really long hex number.For example one hex number with 300 digits.
    And thanks to the other guys ,I am just checking them now.
    That was really fast answers.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    I checked all of them but unfortunatly as I said they can't convert a one huge hex to dec.
    They are just able to convert the small ones(Max 16 digits)But I want more.
    Thank You

  7. #7
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    After 30 mts, I got it

    in a module past following

    Code:
    Option Explicit
    
    
    
    Public Function BinaryToDecimal(Binary As String) As Long
    Dim n As Long
    Dim s As Integer
    
        For s = 1 To Len(Binary)
            n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
                (s - 1)))
        Next s
    
        BinaryToDecimal = n
    End Function
    
    Public Function DecimalToBinary(DecimalNum As Long) As _
        String
    Dim tmp As String
    Dim n As Long
    
        n = DecimalNum
    
        tmp = Trim(Str(n Mod 2))
        n = n \ 2
    
        Do While n <> 0
            tmp = Trim(Str(n Mod 2)) & tmp
            n = n \ 2
        Loop
    
        DecimalToBinary = tmp
    End Function
    
    
    Public Function HextoBinary(HexVal As String) As String
        Dim binVal As String
        If Len(HexVal) = 1 Then
            binVal = Hex_MAP_BIN(HexVal)
            If binVal <> "" Then
                HextoBinary = binVal
                Debug.Print HexVal & "-" & binVal
            Else
                Exit Function
            End If
        Else
        
             Debug.Print Mid(HexVal, Len(HexVal), 1)
             Debug.Print Mid(HexVal, 1, Len(HexVal) - 1)
            binVal = HextoBinary(Mid(HexVal, 1, Len(HexVal) - 1)) & HextoBinary(Mid(HexVal, Len(HexVal), 1))
            HextoBinary = binVal
           
        End If
        
        
        
    End Function
    
    
    Public Function Hex_MAP_BIN(HexVal As String) As String
        Select Case UCase(HexVal)
            Case "0"
                Hex_MAP_BIN = "0000"
            Case "1"
                Hex_MAP_BIN = "0001"
            Case "2"
                Hex_MAP_BIN = "0010"
            Case "3"
                Hex_MAP_BIN = "0011"
            Case "4"
                Hex_MAP_BIN = "0100"
            Case "5"
                Hex_MAP_BIN = "0101"
            Case "6"
                Hex_MAP_BIN = "0110"
            Case "7"
                Hex_MAP_BIN = "0111"
            Case "8"
                Hex_MAP_BIN = "1000"
            Case "9"
                Hex_MAP_BIN = "1001"
            Case "A"
                Hex_MAP_BIN = "1010"
            Case "B"
                Hex_MAP_BIN = "1011"
            Case "C"
                Hex_MAP_BIN = "1100"
            Case "D"
                Hex_MAP_BIN = "1101"
            Case "E"
                Hex_MAP_BIN = "1110"
            Case "F"
                Hex_MAP_BIN = "1111"
            Case Else
            
        End Select
        
    End Function
    
    Public Function HexToDecimal(HexVal As String) As Long
        Dim binVal As String
        Dim decVal As Long
        
        binVal = HextoBinary(HexVal)
        If binVal = "01011110101101010010" Then MsgBox "OK"
        
        
        decVal = BinaryToDecimal(binVal)
        HexToDecimal = decVal
    End Function
    in a command button

    Code:
    Private Sub Command2_Click()
        MsgBox HexToDecimal("5EB52")
    
    
    End Sub
    tel me if this works

    IIF(Post.Rate > 0 , , )

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by dbboy
    Yes ,I mean 0ne really long hex number.For example one hex number with 300 digits.
    And thanks to the other guys ,I am just checking them now.
    That was really fast answers.
    A hex number with 300 digits is so large that it isn't a meaningful number any longer; there is absolutely nothing useful you can do with it.

    To put it in perspective, Planck's constant is the measure below which you cannot observe without disturbing what you're observing because it's smaller than the wavelength of light. ie: Sub-atomic particles. Planck units also apply to time; you can think of it as an instant of time so short that it is indivisible; a true "moment".

    The four-dimensional space-time volume of the history of the known universe -- the spatial volume in unimaginably small units * the age in indivisibly small increments -- is on the scale of 10^245. And you're asking about 16^300.

    Let us know if you manage to do it, because if you do then we can start exhaustively enumerating every moment in the life of every subatomic particle that has ever existed in the history of the universe.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by zeezee
    After 30 mts, I got it

    in a module past following

    Code:
    Option Explicit
    
    
    
    Public Function BinaryToDecimal(Binary As String) As Long
    Dim n As Long
    Dim s As Integer
    
        For s = 1 To Len(Binary)
            n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
                (s - 1)))
        Next s
    
        BinaryToDecimal = n
    End Function
    
    Public Function DecimalToBinary(DecimalNum As Long) As _
        String
    Dim tmp As String
    Dim n As Long
    
        n = DecimalNum
    
        tmp = Trim(Str(n Mod 2))
        n = n \ 2
    
        Do While n <> 0
            tmp = Trim(Str(n Mod 2)) & tmp
            n = n \ 2
        Loop
    
        DecimalToBinary = tmp
    End Function
    
    
    Public Function HextoBinary(HexVal As String) As String
        Dim binVal As String
        If Len(HexVal) = 1 Then
            binVal = Hex_MAP_BIN(HexVal)
            If binVal <> "" Then
                HextoBinary = binVal
                Debug.Print HexVal & "-" & binVal
            Else
                Exit Function
            End If
        Else
        
             Debug.Print Mid(HexVal, Len(HexVal), 1)
             Debug.Print Mid(HexVal, 1, Len(HexVal) - 1)
            binVal = HextoBinary(Mid(HexVal, 1, Len(HexVal) - 1)) & HextoBinary(Mid(HexVal, Len(HexVal), 1))
            HextoBinary = binVal
           
        End If
        
        
        
    End Function
    
    
    Public Function Hex_MAP_BIN(HexVal As String) As String
        Select Case UCase(HexVal)
            Case "0"
                Hex_MAP_BIN = "0000"
            Case "1"
                Hex_MAP_BIN = "0001"
            Case "2"
                Hex_MAP_BIN = "0010"
            Case "3"
                Hex_MAP_BIN = "0011"
            Case "4"
                Hex_MAP_BIN = "0100"
            Case "5"
                Hex_MAP_BIN = "0101"
            Case "6"
                Hex_MAP_BIN = "0110"
            Case "7"
                Hex_MAP_BIN = "0111"
            Case "8"
                Hex_MAP_BIN = "1000"
            Case "9"
                Hex_MAP_BIN = "1001"
            Case "A"
                Hex_MAP_BIN = "1010"
            Case "B"
                Hex_MAP_BIN = "1011"
            Case "C"
                Hex_MAP_BIN = "1100"
            Case "D"
                Hex_MAP_BIN = "1101"
            Case "E"
                Hex_MAP_BIN = "1110"
            Case "F"
                Hex_MAP_BIN = "1111"
            Case Else
            
        End Select
        
    End Function
    
    Public Function HexToDecimal(HexVal As String) As Long
        Dim binVal As String
        Dim decVal As Long
        
        binVal = HextoBinary(HexVal)
        If binVal = "01011110101101010010" Then MsgBox "OK"
        
        
        decVal = BinaryToDecimal(binVal)
        HexToDecimal = decVal
    End Function
    in a command button

    Code:
    Private Sub Command2_Click()
        MsgBox HexToDecimal("5EB52")
    
    
    End Sub
    tel me if this works

    Unfortunatly NO!
    If you want to test it insted of MsgBox HexToDecimal("5EB52")
    Put MsgBox HexToDecimal("aaaaaaaaaa")
    And you will see it's gonna give you the overflow error.

  10. #10
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    hey sorry fellow, there's an overflow I ll try to fix it.
    BTW do you knopw any data taype bigger than long???
    IIF(Post.Rate > 0 , , )

  11. #11
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Convert huge Hex number to Decimal

    Decimal is bigger than long, but it's not a natice type; it's a variant sub-type. Currency and double are also bigger than Long, but Decimal is the biggest.

  12. #12
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Convert huge Hex number to Decimal

    Good luck storing that large of a number into an integer/long/double/currency variable.

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by Ellis Dee
    A hex number with 300 digits is so large that it isn't a meaningful number any longer; there is absolutely nothing useful you can do with it.

    To put it in perspective, Planck's constant is the measure below which you cannot observe without disturbing what you're observing because it's smaller than the wavelength of light. ie: Sub-atomic particles. Planck units also apply to time; you can think of it as an instant of time so short that it is indivisible; a true "moment".

    The four-dimensional space-time volume of the history of the known universe -- the spatial volume in unimaginably small units * the age in indivisibly small increments -- is on the scale of 10^245. And you're asking about 16^300.

    Let us know if you manage to do it, because if you do then we can start exhaustively enumerating every moment in the life of every subatomic particle that has ever existed in the history of the universe.
    Please be more clear.
    You mean there is no way to calculate that number(Which I belive there is) or you mean there is a way but That number is not useful!
    I am waiting for your answer because if there is not I have to think about something else.

  14. #14
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by dbboy
    Unfortunatly NO!
    If you want to test it insted of MsgBox HexToDecimal("5EB52")
    Put MsgBox HexToDecimal("aaaaaaaaaa")
    And you will see it's gonna give you the overflow error.
    well no, this is just an example.
    of course there is a limit, you know that data types have limits.

    I made a change
    do it and see
    Code:
    Public Function BinaryToDecimal(Binary As String) As Double
    Dim n As Double
    Dim s As Long
    
        For s = 1 To Len(Binary)
            n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
                (s - 1)))
        Next s
    
        BinaryToDecimal = n
    End Function
    Code:
    Public Function HexToDecimal(HexVal As String) As Double
        Dim binVal As String
        Dim decVal As Double
        
        binVal = HextoBinary(HexVal)
        If binVal = "01011110101101010010" Then MsgBox "OK"
        
        
        decVal = BinaryToDecimal(binVal)
        HexToDecimal = decVal
    End Function
    and the command goes
    Code:
    Private Sub Command2_Click()
        MsgBox HexToDecimal("2134325423423452354765785609234defaffbc9049823984908234")
    
    
    End Sub

    i ll put the output
    Attached Images Attached Images  
    IIF(Post.Rate > 0 , , )

  15. #15
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Convert huge Hex number to Decimal

    Pretending for the moment that we're talking about base 10 instead of base 16:

    Currency tops out at ~ 10^15 (922,337,203,685,477)
    Decimal tops out at ~ 10^29 (79,228,162,514,264,337,593,543,950,335)
    Double can handle ~ 10^308 (1.79769313486232E308), but it only maintains just over a dozen significant digits.

  16. #16
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Convert huge Hex number to Decimal

    For 16^300, C++ outputs:

    1.#INF

  17. #17
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by dbboy
    Please be more clear.
    You mean there is no way to calculate that number(Which I belive there is) or you mean there is a way but That number is not useful!
    I am waiting for your answer because if there is not I have to think about something else.
    You can't treat it as a number. You will have to write your own library from scratch to do it. Such a library will have to handle the numbers as strings, and you'll need to code all mathematical operations the long way, just like we all learned in elementary school.

    "80256321532" +
    "65487213654" =

    4+2=6
    5+3=8
    6+5=1 (carry the 1!)
    3+1=4+1=5
    1+2=3
    3+2=5
    6+7=3 (carry the 1!)
    8+5=3+1=4 (carry the 1!)
    4+2=6+1=7
    5+0=5
    6+8=4 (carry the 1!)

    =

    145743535186

    That's the easy part, of course. Multiplication and long division will be a mess, and god help you if you need to do something crazy like multiply by an irrational number or find the square root.

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by zeezee
    well no, this is just an example.
    of course there is a limit, you know that data types have limits.

    I made a change
    do it and see
    Code:
    Public Function BinaryToDecimal(Binary As String) As Double
    Dim n As Double
    Dim s As Long
    
        For s = 1 To Len(Binary)
            n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
                (s - 1)))
        Next s
    
        BinaryToDecimal = n
    End Function
    Code:
    Public Function HexToDecimal(HexVal As String) As Double
        Dim binVal As String
        Dim decVal As Double
        
        binVal = HextoBinary(HexVal)
        If binVal = "01011110101101010010" Then MsgBox "OK"
        
        
        decVal = BinaryToDecimal(binVal)
        HexToDecimal = decVal
    End Function
    and the command goes
    Code:
    Private Sub Command2_Click()
        MsgBox HexToDecimal("2134325423423452354765785609234defaffbc9049823984908234")
    
    
    End Sub

    i ll put the output
    Man you are the best .
    I owe you one.
    It's working Perfectly.
    You can't understand how happy I am .
    Thaaaaaaaaaank You very much!

  19. #19
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    well , I tried with MsgBox HexToDecimal("aaaaaaaaaaaaaaaaa")
    here is the output again.
    DigiRev I gave only a method or an Algorithm for you to try. you can use it with any laguage and any data type. Thats up to you. First you asked for 300 Hex values , but this is closer i guess.
    Check THIS also

    Attached Images Attached Images  
    IIF(Post.Rate > 0 , , )

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by Ellis Dee
    You can't treat it as a number. You will have to write your own library from scratch to do it. Such a library will have to handle the numbers as strings, and you'll need to code all mathematical operations the long way, just like we all learned in elementary school.

    "80256321532" +
    "65487213654" =

    4+2=6
    5+3=8
    6+5=1 (carry the 1!)
    3+1=4+1=5
    1+2=3
    3+2=5
    6+7=3 (carry the 1!)
    8+5=3+1=4 (carry the 1!)
    4+2=6+1=7
    5+0=5
    6+8=4 (carry the 1!)

    =

    145743535186

    That's the easy part, of course. Multiplication and long division will be a mess, and god help you if you need to do something crazy like multiply by an irrational number or find the square root.
    It's look like you know math perfectly.
    But as you can see our freind Zeezee just gave the source code and it's working perfectly.
    You mean this number is not the actuall number which I expected to have.
    by the way I will test it in my way and let you know and thank you very much for your useful information ,I think I have to research more about the things you just mentioned.

  21. #21
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    Yeeeeeeeeeeeeeeeeee ... ha ...
    but this may have limitations, be careful to check for overflows.
    and there is a loop also
    I m happy . because you are happy.


    Credit goes to THIS also


    IIF(Post.Rate > 0 , , )

  22. #22
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Convert huge Hex number to Decimal

    heh, I guess the last 285 digits don't matter.

  23. #23
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by Ellis Dee
    You can't treat it as a number. You will have to write your own library from scratch to do it. Such a library will have to handle the numbers as strings, and you'll need to code all mathematical operations the long way, just like we all learned in elementary school.

    "80256321532" +
    "65487213654" =

    4+2=6
    5+3=8
    6+5=1 (carry the 1!)
    3+1=4+1=5
    1+2=3
    3+2=5
    6+7=3 (carry the 1!)
    8+5=3+1=4 (carry the 1!)
    4+2=6+1=7
    5+0=5
    6+8=4 (carry the 1!)

    =

    145743535186

    That's the easy part, of course. Multiplication and long division will be a mess, and god help you if you need to do something crazy like multiply by an irrational number or find the square root.
    Yeah, someone's already came up with the idea, haven't tried it myself though:
    http://www.pscode.com/vb/scripts/Sho...62738&lngWId=1

  24. #24
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by dbboy
    You mean this number is not the actuall number which I expected to have.
    It is not even remotely close to the number. It only has 15 significant digits. To put that into context for you, throw this function into that module and run it:
    Code:
    Public Sub Imprecision()
        Dim dblNumber  As Double
        Dim dblCompare As Double
        
        dblNumber = HexToDecimal("2134325423423452354765785609234defaffbc9049823984908234")
        dblCompare = dblNumber - 10000000000000#
        If dblNumber = dblCompare Then
            MsgBox "x minus 10,000,000,000,000 is equal to x"
        End If
    End Sub

  25. #25
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by DigiRev
    Yeah, someone's already came up with the idea, haven't tried it myself though:
    http://www.pscode.com/vb/scripts/Sho...62738&lngWId=1
    This, on the other hand, is exactly what you're looking for.

  26. #26
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by Ellis Dee
    This, on the other hand, is exactly what you're looking for.
    Yep , you are right. This is just what you want.
    Its a bit similar to what I did, like first convert to base2 then to base10, but this is better.
    I did this one by looking into that wiki article. I see that this method uses just the same method to convert from base to base using the algorithm given there.

    I tested both codes, they work fine, my one seems bit speadier though.
    test the both.

    IIF(Post.Rate > 0 , , )

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by Ellis Dee
    This, on the other hand, is exactly what you're looking for.
    Did you test this one?
    I just test it but it's look like it will not give you the correct answer.
    Just test it with 10 "a" and select from base 16 to base 10 ,then copy the result and change the setting vise versa ,it won't give you the correct answer.
    If I am wrong please let me know

  28. #28
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    I tried, it works for me. try to use the Scientific Calculator to get the value.
    On myprog, that prog and Cal, same answer came.
    IIF(Post.Rate > 0 , , )

  29. #29

  30. #30
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    wish there'd been a SizeOf operator for VB6 also. They have it on .net
    Y didnt they think of it earlier.

    IIF(Post.Rate > 0 , , )

  31. #31

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    Ok I took Screen Shots as u can see the first pic is convert 4 "a" from 16 to 10 and I copied the result and tried to convert that number from base 10 to base 16 ,but you see the result.
    What's wrong?
    Am I doing smt wrong?



    Attached Images Attached Images   

  32. #32
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Convert huge Hex number to Decimal

    I don't really understand hex too well, but my guess is that hex "aaaa" is a negative number, which isn't supported in that program. (Neither are decimals.) I only say aaaa might be a negative number because of the debug window, but I'm probably doing it wrong:
    Code:
    ?&haaaa
    -21846

  33. #33

    Thread Starter
    Junior Member
    Join Date
    Aug 2007
    Location
    NY
    Posts
    26

    Re: Convert huge Hex number to Decimal

    Got it!
    You have to enter your alphabet as capital!
    So if you put "AAAAA" and vise versa it's gonna work .
    I don't know how to say thanks to you guys ,but I really apreciate your help and I hope I can help you in the future.
    But I am not finish ,for sure I will come back with other question.
    Especial thanks to AAAAAAAALLLLLLLLLLLLLLLLLL!

  34. #34
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by Ellis Dee
    I don't really understand hex too well, but my guess is that hex "aaaa" is a negative number.
    Code:
    ?&HAAAA ' Integer
    -21846 
    ?&HAAAA& ' Long
     43690
    Hexidecimal 1234 is:

    Code:
      4 * 160 |      4
    + 3 * 161 | +   48
    + 2 * 162 | +  512
    + 1 * 163 | + 4096
    = 4660
    In hexidecimal A thru F are equivalent to decimal 10 thru 15, so hexidecimal AAAA is:
    Code:
      10 * 160 |      10
    + 10 * 161 | +   160
    + 10 * 162 | +  2560
    + 10 * 163 | + 40960
    = 43960
    I would imagine that it shouldn't be too difficult to create a function that would take a hexidecimal string of arbitrary length and return a decimal string of appropriate length, including converting a 300-digit hexidecimal number into an equivalent 360- or 361-digit decimal number. Note that the return type of such a function would have to be String.

  35. #35
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    Quote Originally Posted by dbboy
    Got it!
    You have to enter your alphabet as capital!
    So if you put "AAAAA" and vise versa it's gonna work .
    I don't know how to say thanks to you guys ,but I really apreciate your help and I hope I can help you in the future.
    But I am not finish ,for sure I will come back with other question.
    Especial thanks to AAAAAAAALLLLLLLLLLLLLLLLLL!

    well , there is something I did for this in my progrma, I convert the Characters to upper case for comparison.
    That way there wont be any problem, May be for the hex tool also you have to something like that.

    IIF(Post.Rate > 0 , , )

  36. #36
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Convert huge Hex number to Decimal

    I was right. After a bit of brainstorming, this was not difficult to code.
    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      Dim strHex As String
      Dim dblVar As Double
      Dim n As Long
      Dim msg As String
      
      strHex = InputBox("Please enter a large hexidecimal value.", "HexToDec Test")
      On Error Resume Next
      dblVar = CDbl("&H" & Left(strHex, 15))
      On Error GoTo 0
      n = Len(strHex) - 15
      If n > 0 Then dblVar = dblVar * 16 ^ n
      msg = CStr(dblVar) & vbCrLf & HexToDec(strHex)
      MsgBox msg, vbOKOnly, "HexToDec Test"
    End Sub
    
    Public Function HexToDec(strHex As String) As String
      Const DigitRatio = 1.20411998265592 ' = Log(16) / Log(10)
      Dim btDigit() As Byte
      Dim lngMaxDigit As Long ' To keep track of actual number of decimal digits
      Dim lngCarry As Long ' For carry-over during additions
      Dim i As Long, j As Long
      Dim btTemp As Byte
      
      ' Make array large enough to hold all digits
      ReDim btDigit(Int(Len(strHex) * DigitRatio))
      For i = 1 To Len(strHex) ' Loop through the string
        ' Check for valid hexidecimal digit and assign appropriate value to lngCarry
        j = Asc(Mid(strHex, i, 1))
        Select Case j
        Case 48 To 57 ' 0 to 9
          lngCarry = j - 48
        Case 65 To 70 ' A to F
          lngCarry = j - 55
        Case 97 To 102 ' a to f
          lngCarry = j - 87
        Case Else ' not a valid hexidecimal digit
          Exit For
        End Select
        
        ' Multiply each decimal digit by 16 and add previous carry.
        ' This effectively multiplies the entire number by 16
        ' and adds the value of the current hexidecimal digit.
        For j = 0 To lngMaxDigit
          btDigit(j) = btDigit(j) * 16 + lngCarry
          lngCarry = btDigit(j) \ 10
          btDigit(j) = btDigit(j) Mod 10
        Next j
        ' Continue until there is nothing to carry over, adding digits as necessary
        Do Until lngCarry = 0
          lngMaxDigit = lngMaxDigit + 1
          btDigit(lngMaxDigit) = lngCarry Mod 10
          lngCarry = lngCarry \ 10
        Loop
      Next i
      
      ' Remove any leading zeros
      If lngMaxDigit < UBound(btDigit) Then
        ReDim Preserve btDigit(lngMaxDigit)
      End If
      
      ' Convert digit value to character code
      For i = 0 To lngMaxDigit
        btDigit(i) = btDigit(i) + 48
      Next i
      
      ' Reverse the order of the digits
      i = 0: j = lngMaxDigit
      Do While i < j
        btTemp = btDigit(i)
        btDigit(i) = btDigit(j)
        btDigit(j) = btTemp
        i = i + 1
        j = j - 1
      Loop
      
      'Convert to string and return
      HexToDec = StrConv(btDigit, vbUnicode)
    End Function

  37. #37
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: Convert huge Hex number to Decimal

    you could try this too:

    From hexidecimal to decimal, max number convertable approximately 33B2E3C9FD0803CE7FFFFFF
    Code:
    Function HexToDec(ByVal sHex As String) As Variant
      Dim ba() As Byte, i&, bitval As Variant, b As Byte
     bitval = 1
     ba = sHex
     For i = UBound(ba) - 1 To 0 Step -2
        b = ba(i)
        If b < 58 Then HexToDec = CDec(HexToDec + (b - 48) * bitval) Else HexToDec = CDec(HexToDec + (b - 55) * bitval)
        bitval = CDec(bitval * 16)
     Next i
    End Function

  38. #38
    Banned learning c's Avatar
    Join Date
    Mar 2007
    Location
    canberra (australia's capital)
    Posts
    198

    Re: Convert huge Hex number to Decimal

    same principles apply to any language tho except that implementation is optimised for vb by using byte arrays

  39. #39
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Convert huge Hex number to Decimal

    Just out of curiosity, what kind of data are you dealing with that could possibly refer to a number so large?

  40. #40
    Frenzied Member
    Join Date
    Jul 2007
    Posts
    1,306

    Re: Convert huge Hex number to Decimal

    well I coud convert
    FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

    with the Conversion tools and my program both.
    But to speak the thruth, the conversion progrma took some while to convert than my one. If you are considering for performance iseues, then I ll stick to my one.

    IIF(Post.Rate > 0 , , )

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width