PDA

View Full Version : [SOLVED] Converting large decimals to hex



BexleyManor
02-24-2005, 02:25 AM
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??

:dunno

Jacob Hilderbrand
02-24-2005, 04:28 AM
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

BexleyManor
02-25-2005, 07:22 AM
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)

tinyjack
02-25-2005, 09:56 AM
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

Jacob Hilderbrand
02-25-2005, 02:29 PM
Check out these KB Entries. You can use them together to get your result.

DecToHex (http://www.vbaexpress.com/kb/getarticle.php?kb_id=306)
HexToDec (http://www.vbaexpress.com/kb/getarticle.php?kb_id=307)

Then use both Functions like this:


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

BexleyManor
02-25-2005, 03:34 PM
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!!

Jacob Hilderbrand
02-25-2005, 03:52 PM
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

BexleyManor
02-25-2005, 04:17 PM
ha ha ha, now you're just showing off 'o great one!!

Jacob Hilderbrand
02-25-2005, 04:22 PM
Glad to help. :beerchug:

Take Care