Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
This code converts any base number to any base num...
Author:
Amit Ginotra
E-mail:
Click to e-mail author
Submitted:
6/5/2003
Version:
VB3
Compatibility:
VB3, VB4, VB5, VB6
Category:
Mathematics
Views:
8538
This code converts any base number to any base number system. Bases between 2 to 36 are allowed.
Declarations:
Private Const CharCodeMap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" Public Const aguERR_INVALIDPARAMETERVALUE = vbObjectError + 500 + 1
Code:
Public Function BaseX2Y(ByVal X As Integer, ByVal Y As Integer, ByVal xValue As String) As String Dim intLoop As Integer If X < 2 Or X > 36 Or _ Y < 2 Or Y > 36 Then _ Err.Raise aguERR_INVALIDPARAMETERVALUE, , GetErrorString(aguERR_INVALIDPARAMETERVALUE) If xValue = vbNullString Then Err.Raise aguERR_INVALIDPARAMETERVALUE, , GetErrorString(aguERR_INVALIDPARAMETERVALUE) Dim lngCharPos As Long For intLoop = 1 To Len(xValue) lngCharPos = InStr(1, CharCodeMap, Mid(xValue, intLoop, 1), vbTextCompare) If lngCharPos > X Or lngCharPos <= 0 Then _ Err.Raise aguERR_INVALIDPARAMETERVALUE, , GetErrorString(aguERR_INVALIDPARAMETERVALUE) Next Dim strOutValue As String Dim lngInValue As Long Dim lngRemainder As Long lngInValue = 0 If X <> 10 Then Dim intLen As Integer intLen = Len(xValue) - 1 For intLoop = 0 To intLen lngInValue = lngInValue + (Val(InStr(1, CharCodeMap, Mid(xValue, intLoop + 1, 1), vbTextCompare) - 1) * (X ^ (intLen - intLoop))) Next Else lngInValue = Val(xValue) End If If lngInValue = 0 Then BaseX2Y = "0": Exit Function If Y = 10 Then BaseX2Y = Trim$(Str$(lngInValue)): Exit Function While lngInValue > 0 lngRemainder = lngInValue Mod Y lngInValue = lngInValue \ Y strOutValue = Mid$(CharCodeMap, lngRemainder + 1, 1) & strOutValue Wend BaseX2Y = strOutValue End Function Public Function GetErrorString(ByVal ErrNumber As Long) As String GetErrorString = "" End Function
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2023 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement