with: Erik Oosterwal
Custom Search
|
Function base2base(intInputBase As Integer, intOutputBase As Integer, txtValue
As String) As String
Dim J, K, DecimalValue, X, MaxBase, InputNumberLength As Integer
Dim NumericBaseData, OutputValue As String
NumericBaseData = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MaxBase = Len(NumericBaseData)
If (intInputBase > MaxBase) Or (intOutputBase > MaxBase) Then
base2base = "#N/A"
Exit Function
End If
'*/ Convert InputNumber to Base 10 /*
InputNumberLength = Len(txtValue)
DecimalValue = 0
For J = 1 To InputNumberLength
For K = 1 To intInputBase
If Mid(txtValue, J, 1) = Mid(NumericBaseData, K, 1) Then
DecimalValue = DecimalValue + Int((K - 1) * (intInputBase ^ (InputNumberLength- J)) + 0.5)
End If
Next K
Next J
'*/ Convert the Base 10 value (DecimalValue) to the desired output base /*
OutputValue = ""
While DecimalValue > 0
X = Int(((DecimalValue / intOutputBase) - Int(DecimalValue / intOutputBase)) * intOutputBase + 1.5)
OutputValue = Mid(NumericBaseData, X, 1) + OutputValue
DecimalValue = Int(DecimalValue / intOutputBase)
Wend
base2base = OutputValue
End Function
Once you return to Excel, you can use this function just like you would any other math function, like =sum() or =avg(), you just have to remember to preface it with 'Module1'. The formula in your cell would look something like:
=module1.base2base(B7, C7, B8)
This will convert the value stored in cell B8 from the base in cell B7 to the base in cell C7. The output from this function is a string, so you might have to format the cells to be right justified in order to get numbers to line up correctly.