Search Tools Links Login

Convert a Decimal Number in a Given Base


Just a little something to help you do math in different numbering systems.

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

About this post

Posted: 2019-08-23
By: AndreaTincani
Viewed: 275 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Special Instructions

This code originally appeared on AndreaVB.com, and has been republished here with the permission of Andrea Tincani.


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.