Convert numbers to any base
By: Lorenzo Dalla Vecchia Date: 19/07/2001


1.10 KB

Download now!

The numbers we are used to work with are in decimal base. That means there are 10 different digits to work with, and so we need one digit for numbers lower than the first power of 10 (10), two digits for numbers until the second power of 10 (100), three for numbers until the third power of 10 (1000) and so on.

In the computer world, there are also other numeric systems used: binary (with two digits, 0-1), hexadecimal (with 16 digits, 0-F) and octal (with 8 digits, 0-7).

This Visual Basic module, contains a function that goes beyond the usual base-conversion between the 4 bases: it can, if fact, convert numbers from any base to any base, The only limit is that they must be at least binary (the lowest base) and that the maximum number of digits is 36 (0-9 and A-Z). However you can edit the code and add more symbols for more digits.

The use and syntax is pretty simple.


BaseConv([NewBase], OldNumber, [OldBase], [RetainDecimals])


NewBase: base to convert OldNumber into; dafault is 10.
OldNumber: the number to be converted.
OldBase: original base of OldNumber; default is 10.
RetainDecimals: converts the digits after the dot; default is False.

Type of data

NewBase: Integer
OldNumber: Variant
OldBase: Integer
RetainDecimals: Boolean


A Variant expression containing the converted number or ERR if an error occoured.


Example 1:
Convert decimal 37 into binary.

MsgBox BaseConv(2, 37)
'result is 100101

Example 2:
Convert octal 21 into decimal.

MsgBox BaseConv(, 21, 8)
'result is 17

Example 3:
Convert convert 20-based H86AE into hexadecimal.

MsgBox BaseConv(16, "H86AE", 20)
'result is "2AB536"

The function is contained in a module. You can include it in any program of yours.

Click Here!

Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: