Brain Teaser that I am having issues with

jc.021286

Well-known Member
Joined
Apr 12, 2010
Messages
725
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.

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
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Thank you!

That was what I was looking for.
Now under the old adage, give a man a fish, he eats for a day, teach a man to fish, he eats for a lifetime.

Could you explain a bit of what is going on?

Here is what I understand,
Code:
<code>Const PossibleDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"</code>
This is going to be used to pull the digits for the number to be displayed, so if it exceeds base 10, it'll start going into the ABC...XYZ before maxing out at the base value and cycling back through.

then could you explain the usage of
Code:
<code>Do Until DecimalValue = 0     Dec2Base = Mid(PossibleDigits, CDec(DecimalValue) - Base * _                Int(DecimalValue / Base) + 1, 1) & Dec2Base     DecimalValue = Int(CDec(DecimalValue) / Base)   Loop</code>

You are going into the constant with the mid function and pulling 1 letter. Based on the
value - Base * (value / Base) + 1
Then tack this onto the front of the number before.
So you are building it from Left to right
Then you adjust the value down by the base...

I'm getting confused by just trying to work it out. Could you describe the logic?
The Int(DecimalValue/Base) does this need to be a rounddown or up function? Or is that automatically handled by casting into the Int?
</pre>
</pre>
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,912
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