hexadecimal calculator using Do Until-loop and select case

bglanton

New Member
Joined
Oct 3, 2014
Messages
10
I am trying to write a macro that converts a decimal number into a hexadecimal using Do Until-loop and select case
here is what I have so far when I run it the computer freezes up

Sub HexConverter()

Dim L As Double
Dim K As Integer
Dim N As Double
Dim R As Double
Dim A As Double
Dim P As String
Dim Hex As String
Dim HexRev As String
Dim i As Integer

N = Range("G5")
InputNum = N

Do
L = N
A = N / 16
K = Int(A)
If K < 16 Then
R = K
Else
R = A - K
R = R * 16
R = Round(R, 0)
End If

If R = 0 Then
P = "0"
Else
Select Case R
Case 1 To 9
P = CStr(R)
Case 10
P = "A"
Case 11
P = "B"
Case 12
P = "C"
Case 13
P = "D"
Case 14
P = "E"
Case 15
P = "F"
End Select
End If

If L = InputNum Then
Hex = P
Else
Hex = Hex & P
End If
Loop Until K = 0

For i = 1 To Len(Hex)
HexRev = Mid(Hex, i, 1) & HexRev
Next i

Range("G9") = HexRev

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The way you phrased your question, it sounds like homework. If that is the case, then the following won't be of much help to you as it uses a different method to calculate the conversion. For those who might need it, though, the following function, which can be used as a UDF (user defined function), can take a positive or negative number for its first argument (although you will need to supply a bit size for the optional second argument if the value is negative) and will handle a number whose conversion can be as much as 93 bits (way, way, way larger than there should ever be need for... but the function handles the calculation quite efficiently no matter what size number is being converted). The maximum decimal argument that can be passed into the first argument is 9999999999999999999999999999 (that is 28 nines), of course once the number get large enough, it would have to be passed in as a text string value to stop Excel or VB (depending on where it is called from) from changing the argument to floating point number. Anyway, here is the code.
Code:
Function BigDec2Hex(ByVal DecimalIn As Variant, Optional BitSize As Long = 93) As String
  Dim X As Integer, PowerOfTwo As Variant, BinaryString As String
  Const BinValues = "*0000*0001*0010*0011*0100*0101*0110*0111*1000*1001*1010*1011*1100*1101*1110*1111*"
  Const HexValues = "0123456789ABCDEF"
  DecimalIn = Int(CDec(DecimalIn))
  If DecimalIn < 0 Then
    If BitSize > 0 Then
      PowerOfTwo = 1
      For X = 1 To BitSize
        PowerOfTwo = 2 * CDec(PowerOfTwo)
      Next
    End If
    DecimalIn = PowerOfTwo + DecimalIn
    If DecimalIn < 0 Then
      BigDec2Hex = CVErr(xlErrValue)
      Exit Function
    End If
  End If
  Do While DecimalIn <> 0
    BinaryString = Trim$(Str$(DecimalIn - 2 * Int(DecimalIn / 2))) & BinaryString
    DecimalIn = Int(DecimalIn / 2)
  Loop
  BinaryString = String$((4 - Len(BinaryString) Mod 4) Mod 4, "0") & BinaryString
  For X = 1 To Len(BinaryString) - 3 Step 4
    BigDec2Hex = BigDec2Hex & Mid$(HexValues, (4 + InStr(BinValues, "*" & Mid$(BinaryString, X, 4) & "*")) \ 5, 1)
  Next
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top