Computer Science 101

with:  Erik Oosterwal

Search for specific programs or algorithms:
Custom Search





Numeric Base Conversion Excel Function


This algorithm is the exact same as the one described in the Numeric Base Conversion on the main Computer Science 101 page.  It's been written to assume the value to be converted will be in a string variables but I found that Excel is pretty forgiving on that and will do the conversion autmatically if you pass an integer value.  The way it's written now, it does not convert fractions, but as soon as that is implemented to the other generic base conversion algorithm I'll update this function to do the same.

To use this algorithm in Excel, follow the instructions below:
  1. Copy the code listed below by highlighting the entire code including the Function and End Function statements, then selecting 'Edit' -Copy' from the menus at the top of the screen or press CTRL-C.
  2. Open Excel.
  3. In Excel, press SHIFT-F11 to go to Visual Basic for Applications.
  4. In Visual Basic for Applications (VBA), click Insert from the menu at the top of the page then click Module.
  5. In VBA, paste the code you copied in step 1 by pressing CTRL-V or selecting 'Edit'-'Paste' from the menus at the top of the screen.
  6. In VBA, press CTRL-S to save the function in your workbook.
  7. In VBA, press ALT-Q to exit VBA and return to your workbook.
  8. In Excel, press SHIFT-F3 to enter a function and select Module1.base2base().
    Module1 may be listed as something else depending on how it was saved from VBA




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.





Discuss computer algorithms and other computer science topics at the Computer Algorithms blog page.

All code and original algorithms are © Erik Oosterwal - 1987-2008
Computer Science 101

Dressing for Success