borderAndreaVB free resources for Visual Basic developersborder
AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2017 Andrea Tincani
:: Convert a decimal number in a given base

Author  

Andrea Tincani

Language  

VB5, VB6

Operating Systems  

Windows 95, 98, NT, 2k, Me and XP
Module
Option Explicit

'Convert a decimal number in another base
Public Function CBase(ByVal number As Long, ByVal base As Long) As String
    'symbols to code the number
    Const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    Dim r As Long

    'verify if there are enought symbols to code the number in the selected base
    If base < 2 Or base > Len(chars) Then Exit Function
    CBase = ""
    Do While number >= base
        r = number Mod base
        CBase = Mid(chars, r + 1, 1) & CBase
        number = number \ base
    Loop
    CBase = Mid(chars, number + 1, 1) & CBase
End Function
Usage
'Create a form with a button and two text box, 
'in the first text box enter the decimal number to convert
'in the second enter the new base in which the number will be converted
Private Sub Command1_Click()
    MsgBox CBase(Val(Text1.Text), Val(Text2.Text))
End Sub
:: Navigation

Home

Beginners Corner

Previous Tip

Next Tip

:: Search this site
Google
:: Related Topics
icon 15-01-2010 Re: Database Saving by GeoffS
icon 23-09-2009 vb newbie has problem by roughgod22
icon 24-07-2007 Please help me by Ghobashi
icon 23-07-2007 Please let me open this code with VB.Net by Ghobashi
icon 30-05-2007 Re: Percentage in listbox by admin
borderAndreaVB free resources for Visual Basic developersborder
borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2017 Andrea Tincaniborder