VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Convert from Decimal to any base, and vice versa, PLUS any base to any base!!!

by TerranRiCH (5 Submissions)
Category: Math/Dates
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Thu 26th July 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

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!!!




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


Download this snippet    Add to My Saved Code

Convert from Decimal to any base, and vice versa, PLUS any base to any base!!! Comments

No comments have been posted about Convert from Decimal to any base, and vice versa, PLUS any base to any base!!!. Why not be the first to post a comment about Convert from Decimal to any base, and vice versa, PLUS any base to any base!!!.

Post your comment

Subject:
Message:
0/1000 characters