VB icon

Base 2-36 conversion

Submitted on: 1/4/2015 11:53:00 AM
By: Joseph Wang (from psc cd)  
Level: Beginner
User Rating: By 9 Users
Compatibility: VB 3.0, VB 4.0 (16-bit), VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 809
     Converts a number from base 2~36 to a number of base 2~36
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
' Name: Base 2-36 conversion
' Description:Converts a number from base 2~36 to a number of base 2~36
' By: Joseph Wang (from psc cd)
' Assumes:use all lower case if passed base 10

Option Explicit
Private Function dec2any(number As Long, convertb As Integer) As String
On Error Resume Next
Dim num As Long
Dim sum As String
Dim carry As Long
sum = ""
num = number
If convertb > 1 And convertb < 37 Then
carry = num Mod convertb
If carry > 9 Then
sum = Chr$(carry + 87) + sum
sum = carry & sum
End If
num = Int(num / convertb)
Loop Until num = 0
dec2any = sum
dec2any = -1
End If
End Function
Private Function any2dec(num As String, Optional numbase As Integer = 10) As Long
On Error Resume Next
Dim sum As Long
Dim length As Integer
Dim count As Integer
Dim digit As String * 1
length = Len(num)
If length > 0 And numbase > 0 And numbase < 37 Then
For count = 1 To length
digit = Mid$(num, count, 1)
If digit <= "9" Then
sum = sum + digit * numbase ^ (length - count)
sum = sum + (Asc(digit) - 87) * numbase ^ (length - count)
End If
Next count
any2dec = sum
any2dec = -1
End If
End Function
Private Function any2any(num1 As String, num1base As Integer, convertbase As Integer) As String
Dim answer As Long
If num1base <> convertbase And num1base > 0 And convertbase > 0 _
And num1base < 37 And convertbase < 37 Then
answer = any2dec(num1, num1base)
any2any = dec2any(answer, convertbase)
any2any = -1
End If
End Function
Private Sub Form_Load()
' example: converts letter z of base 36 to base 2 (binary)
Me.Caption = any2any("z", 36, 2)
End Sub

Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)

To post feedback, first please login.