VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Converts any number system into any number system, up to base 36. Ex: Base 2(binary) to Base 11(Und

by Anonymous (267 Submissions)
Category: Math/Dates
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 6th February 2006
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Converts any number system into any number system, up to base 36. Ex: Base 2(binary) to Base 11(Undecimal), Base 10(decimal) to Base 13.

API Declarations


'Converts from number system into any number system.
' These functions can, for example, convert Base 2(binary) into base 13(tridecimal).
' It could also convert base 15 into base 3.
'
'Currently, the program IS case sensitive, so you could add abcdefg, etc, on to
' gcEQUIV_STRING to allow conversion to/from number systems higher than base 36.
' If you don't want it to be case sensitive, just add a few well-placed instances
' of UCASE() in the conversion functions.
'
'I couldn't find any code to do this, so I wrote my own.
'
'Feel free to use this. All I ask is that you give me credit.
'
'Enjoy!

'



'This string holds the characters we'll use for our number system conversions.
Global Const gcEQUIV_STRING = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Rate Converts any number system into any number system, up to base 36. Ex: Base 2(binary) to Base 11(Und



'This function just yanks the decimal equivalent of a character from gcEQUIV_STRING

    NmbrSys_GetVal = InStr(1, gcEQUIV_STRING, myChar) - 1
End Function

Public Function NmbrSys_FromDecimal(ByVal myNumber As Currency, ByVal myBase As Byte, Optional ByVal myData As String = "") As String
    'This is a recursive function that converts a number from decimal into a specified number system.
    'Syntax:  NmbrSys_FromDecimal(myNumber,myBase)
    '         Where myNumber is the decimal number to be converted, and myBase is
    '         the number system you want to convert into.  myBase cannot be higher
    '         than the length of gcEQUIV_STRING.  If you need to use a number system
    '         higher than base 36, you'll have to add new characters to gcEQUIV_STRING.
    
    Dim myRemainder As Currency
    
    If myNumber / myBase >= 1 Then
        myRemainder = (myNumber Mod myBase)
        myNumber = myNumber / myBase
        If myNumber > myBase Then
            myData = myData & NmbrSys_FromDecimal(Int(myNumber), myBase, myData) & Mid(gcEQUIV_STRING, myRemainder + 1, 1)
        Else
            myData = myData & Mid(gcEQUIV_STRING, Int(myNumber) + 1, 1)
            myData = myData & Mid(gcEQUIV_STRING, myRemainder + 1, 1)
        End If
    Else
        myData = myData & Mid(gcEQUIV_STRING, Int(myNumber) + 1, 1)
    End If
    
    NmbrSys_FromDecimal = myData
End Function

Public Function NmbrSys_ToDecimal(ByVal myData As String, ByVal myBase As Byte) As Currency
'Converts from any number system into base 10 (decimal).
'SYNTAX:    NmbrSys_ToDecimal(myData,myBase)
'   Where myData represents the number to be converted, and myBase represents which
'   number system we're converting from.

    Dim tmpNumber As Currency
    
    For iPosition = 1 To Len(myData)
        tmpNumber = tmpNumber + (NmbrSys_GetVal(Mid(myData, iPosition, 1)) * (myBase ^ (Len(myData) - iPosition)))
    Next iPosition
    
    NmbrSys_ToDecimal = tmpNumber
End Function

Public Function NmbrSys_Convert(ByVal myData As String, ByVal FromBase As Byte, ByVal ToBase As Byte) As String
'Converts from any number system into any number system, limited by the length
'   of gcEQUIV_STRING, returning a string.
'SYNTAX:    NmbrSys_Convert(myData,FromBase,ToBase)
'   Where myData represents the number to be converted, FromBase represents
'   which number system we're converting from, and ToBase represents which number
'   system we are converting to.

    Dim tmpNumber As Currency
    Dim iPosition As Integer
    Dim iCount As Integer
    
    If FromBase <= Len(gcEQUIV_STRING) And ToBase <= Len(gcEQUIV_STRING) Then   'Check to see if we support the bases we received.
        tmpNumber = NmbrSys_ToDecimal(myData, FromBase) 'Convert our number to decimal.
        
        NmbrSys_Convert = NmbrSys_FromDecimal(tmpNumber, ToBase)    'Convert the decimal into the new number system.
    Else
        NmbrSys_Convert = myData    'If an unsupported base number was passed, return the data unscathed.
    End If
End Function


Download this snippet    Add to My Saved Code

Converts any number system into any number system, up to base 36. Ex: Base 2(binary) to Base 11(Und Comments

No comments have been posted about Converts any number system into any number system, up to base 36. Ex: Base 2(binary) to Base 11(Und. Why not be the first to post a comment about Converts any number system into any number system, up to base 36. Ex: Base 2(binary) to Base 11(Und.

Post your comment

Subject:
Message:
0/1000 characters