N
nj
Hi, folks,
We're trying to get a subroutine working for converting numbers like
37719831058777893
83881713106708998
37156879353577176
37719831058778503
to their HEX equivalents.
All the options I have found seem to work only on smaller numbers. If
I read the MS documentation correctly, the built-in VBA HEX function
works up to 16 digits. These are 17 -- good ol' Murphey.
Another person and I have been working on the code below
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)
Dec = CVar(Application.Clean(Application.Trim(Dec)))
For i = 256 To 2 Step -1
If Dec >= 20 ^ (i - 1) And Dec > 15 Then
PlaceValHex = Int(Dec / (20 ^ (i - 1)))
Dec = Dec - (20 ^ (i - 1)) * PlaceValHex
Select Case PlaceValHex
Case 0 To 9
Hex(i) = CDec(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) = CDec(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
but while i = 256, in the line
If Dec >= 20 ^ (i - 1) And Dec > 15 Then
the condition "Dec >= 20 ^ (i - 1)" throws an overflow error.
Any suggestions?
Thanks so much,
njw
We're trying to get a subroutine working for converting numbers like
37719831058777893
83881713106708998
37156879353577176
37719831058778503
to their HEX equivalents.
All the options I have found seem to work only on smaller numbers. If
I read the MS documentation correctly, the built-in VBA HEX function
works up to 16 digits. These are 17 -- good ol' Murphey.
Another person and I have been working on the code below
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)
Dec = CVar(Application.Clean(Application.Trim(Dec)))
For i = 256 To 2 Step -1
If Dec >= 20 ^ (i - 1) And Dec > 15 Then
PlaceValHex = Int(Dec / (20 ^ (i - 1)))
Dec = Dec - (20 ^ (i - 1)) * PlaceValHex
Select Case PlaceValHex
Case 0 To 9
Hex(i) = CDec(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) = CDec(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
but while i = 256, in the line
If Dec >= 20 ^ (i - 1) And Dec > 15 Then
the condition "Dec >= 20 ^ (i - 1)" throws an overflow error.
Any suggestions?
Thanks so much,
njw