Hopefully someone can help,
I am looking to help with creating a VBA function to essentially "rebase a number"
The function would have 2 inputs, the hex number to rebase and the new base.
If I were to do =HEX2ANY(221,2)
this would give the binary value of 221 (11011101)
however if I want to take that same number and =HEX2ANY(221,3) I'm stuck,
I found some code for doing the binary equiv provided at the bottom. Though I have no clue what this line is doing:
I tried to swap out the 2^n to input2^n but that gives an error so I'd need to know more of what is going on.
Essentially I'm looking to take some positive whole numbers and show their value in bases, 2,3,5,7,11,13... 29...
full code so far that needs tweak for other bases
I am looking to help with creating a VBA function to essentially "rebase a number"
The function would have 2 inputs, the hex number to rebase and the new base.
Code:
Public Function HEX2ANY(input1 As Integer, input2 As Integer)
End Function
If I were to do =HEX2ANY(221,2)
this would give the binary value of 221 (11011101)
however if I want to take that same number and =HEX2ANY(221,3) I'm stuck,
I found some code for doing the binary equiv provided at the bottom. Though I have no clue what this line is doing:
Code:
If input1 And (2 ^ n) Then
...
I tried to swap out the 2^n to input2^n but that gives an error so I'd need to know more of what is going on.
Essentially I'm looking to take some positive whole numbers and show their value in bases, 2,3,5,7,11,13... 29...
full code so far that needs tweak for other bases
Code:
Public Function HEX2ANY(input1 As Integer, input2 As Integer)
Const MAXLEN = 30
Dim strBin As String
Dim n As Long
If input1 < 0 Then
strBin = "1"
Else
strBin = "0"
End If
For n = MAXLEN To 0 Step -1
If input1 And (input2 ^ n) Then
strBin = strBin & "1"
Else
strBin = strBin & "0"
End If
Next
HEX2ANY = strBin
End Function