- Home
·
- Math/Dates
·
- Convert from Decimal to any base, and vice versa, PLUS any base to any base!!!
Convert from Decimal to any base, and vice versa, PLUS any base to any base!!!
Convert from Decimal to any base, and vice versa, PLUS any base to any base!!!
Rate Convert from Decimal to any base, and vice versa, PLUS any base to any base!!!
(2(2 Vote))
Public Function NumberEquiv(Digit As String) As Double
If Digit = Trim(Str(Val(Digit))) Then
NumberEquiv = Val(Digit)
ElseIf Digit >= "A" And Digit <= "Z" Then
NumberEquiv = Asc(Digit) - 55
Else
MsgBox "The program tried to find the numeric value of an unknown character.", vbCritical, "Error!!!"
End If
End Function
' Use this function to convert any number from 0-35 into 0-9 or A-Z (for use with base conversions)
Public Function LetterEquiv(Digit As Integer) As String
Select Case Digit
Case i >= 0, Digit < 10
LetterEquiv = Trim(Str(Digit))
Case Is >= 10, Digit <= 35
LetterEquiv = Chr(Digit + 55)
Case Else
LetterEquiv = "?"
End Select
End Function
' Use this function to convert DECIMAL to ANY BASE
Public Function DecToBase(DecimalNumber As Double, Base As Integer) As String
Q = DecimalNumber
r = 0
Strg = ""
Do Until Q = 0
r = Q Mod Base
Q = Int(Q / Base)
Strg = LetterEquiv(r) + Strg
Loop
DecToBase = Strg
End Function
' Use this function to convert ANY BASE to DECIMAL
Public Function BaseToDec(BaseXNumber As String, Base As Integer) As Double
Dim Total As Double
Total = 0
For i = 1 To Len(BaseXNumber)
Total = Total + (NumberEquiv(Mid(BaseXNumber, i - 1, 1)) * Base ^ (Len(BaseXNumber) - i))
Next i
BaseToDec = Total
End Function
'Use this function to convert ANY BASE to ANY BASE
Public Function BaseToBase(BaseXNumber As String, BaseIn As Integer, BaseOut As Integer) As String
BaseToBase = DecToBase(BaseToDec(BaseXNumber, BaseIn), BaseOut)
End Function
Convert from Decimal to any base, and vice versa, PLUS any base to any base!!! Comments
No comments yet — be the first to post one!
Post a Comment