Consulting

Results 1 to 9 of 9

Thread: Converting large decimals to hex

  1. #1

    Converting large decimals to hex

    Hi all, it's been a while so belated happy new year!!

    Ok, this is today's headwreak.

    I need to convert large decimals into hex. When I say large, for example I need 726628 * 9437778 and the result in hex.

    Obviously if I use hex(****), it throws an error because the decimal is outside of the number range.

    Any suggestions folks??


  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Well if 726628 * 9437778 (6857753752584) is equal to 63CB1F9F408 in HEX then I think I have my macro working correctly.

    What exactly do you need such large HEX values for? Just curious.

    Anyways, try this macro. I tested it up to 65536 and then I ran out of rows. But it does seem to be working fine.

    Option Explicit
     
    Public Function DecToHex(Dec As Double) As String
    Dim i As Long
    Dim n As Long
    Dim PlaceValHex As Long
    Dim Hex(1 To 256) As String
    Dim HexTemp As String
    Dim Divisor As Long
    Dec = Int(Dec)
    For i = 256 To 2 Step -1
    If Dec >= 16 ^ (i - 1) And Dec > 15 Then
    PlaceValHex = Int(Dec / (16 ^ (i - 1)))
    Dec = Dec - (16 ^ (i - 1)) * PlaceValHex
    Select Case PlaceValHex
    Case 0 To 9
    Hex(i) = CStr(PlaceValHex)
    Case Is = 10
    Hex(i) = "A"
    Case Is = 11
    Hex(i) = "B"
    Case Is = 12
    Hex(i) = "C"
    Case Is = 13
    Hex(i) = "D"
    Case Is = 14
    Hex(i) = "E"
    Case Is = 15
    Hex(i) = "F"
    End Select
    Else
    Hex(i) = "0"
    End If
    Next i
    PlaceValHex = Dec
    Select Case PlaceValHex
    Case 0 To 9
    Hex(1) = CStr(PlaceValHex)
    Case Is = 10
    Hex(1) = "A"
    Case Is = 11
    Hex(1) = "B"
    Case Is = 12
    Hex(1) = "C"
    Case Is = 13
    Hex(1) = "D"
    Case Is = 14
    Hex(1) = "E"
    Case Is = 15
    Hex(1) = "F"
    End Select
    For i = 256 To 1 Step -1
    If Hex(i) = "0" Then
    Else
    n = i
    Exit For
    End If
    Next i
    For i = n To 1 Step -1
    HexTemp = HexTemp & Hex(i)
    Next i
    DecToHex = HexTemp
    End Function

  3. #3
    Hi Jake, many thanks for the input. Really appreciated.

    Your solution works perfectly!! However I wish to tweak it slightly but got slighty lost in my own attempts!!

    Would it be possible to replace the function dectohex(Dec) to accept the following arguements Convert(Hex*Hex)

    ie

    Convert(1st input hex number * 2nd input hex numer)

  4. #4
    VBAX Regular
    Joined
    Jun 2004
    Posts
    14
    Location
    Could you just do this:

    Sub Maybe()
    Dim strNum1 As String
    Dim strNum2 As String
    strNum1 = "B1664"
    strNum2 = "900252"
    MsgBox LongHex(CDbl("&h" & strNum1) * CDbl("&h" & strNum2))
    End Sub
     
    Public Function LongHex(dblNumber As Double) As String
    Dim dblRemaining As Double
    Dim dblBalance As Double
    Dim strTemp As String
    Const BLOCK As Double = 268435456
    dblBalance = dblNumber
    Do While dblBalance <> 0
    dblRemaining = dblBalance
    dblBalance = Int(dblBalance / BLOCK)
    dblRemaining = dblRemaining - (dblBalance * BLOCK)
    If dblBalance <> 0 Then
    strTemp = Right("0000000" & Hex(dblRemaining), 7) & strTemp
    Else
    strTemp = Hex(dblRemaining) & strTemp
    End If
    LoopLongHex = strTemp
    End Function

    TJ
    Oh dear, I need a beer

  5. #5
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Check out these KB Entries. You can use them together to get your result.

    DecToHex
    HexToDec

    Then use both Functions like this:

    x=HexToDec(Num1)
    y=HexToDec(Num2)
    z=DecToHex(Num1*Num2)

  6. #6

    Thumbs up

    Thanks Jake, I checked my mail prior to reading what you had added. I will attempt your solution before pestering you all again!!

    **Update**

    Went with what you said an mix and matched as follows: dectohex(hextodec(900252)*726628)

    Worked an absolute treat!!

    There's a cold beer waiting for you on the bar, muchos thanks, another VBAExpress success story!!

  7. #7
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    No problem. You can also make a new UDF if you wanted.


    Public Function HexToHex(Hex1 As string, Hex2 As String) As String
    Dim Dec1 As Double
    Dim Dec2 As Double
    Dec1 = HexToDec(Hex1)
    Dec2 = HexToDec(Hex2)
    HexToHex = DecToHex(Dec1*Dec2)
    End Function

  8. #8
    ha ha ha, now you're just showing off 'o great one!!

  9. #9
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Glad to help.

    Take Care

Posting Permissions

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