Visual Basic Code of the Week (COTW)
http://www.codeoftheweek.com
Issue #69
Online Version at http://www.codeoftheweek.com/membersonly/bi/0069.html (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

The winner for our December 1998 Contest

Timothy Blek was the winner of the free Graphics Server software. Congratulations! We are giving away another copy this month, so if you haven't yet entered, do it at http://www.codeoftheweek.com/contest.html now! If you entered last month, please be sure to enter again because your entry from last month does not count towards the prize this month.

In this Issue

This issue we are introducing a routine to convert a number from one base to another. For example base 16 (hexadecimal) to base 2 (binary).

This source code is designed for VB 3.0 and up. Pedro Dias submitted most of this source code. Questions? Email us at questions@codeoftheweek.com.

AnyBase

The AnyBase function has three parameters: the value to convert, the base the number is in and the base you want to convert to. Some common base values that you might use are 2 for binary, 10 for decimal and 16 for hexadecimal. This routine supports conversions from Base 2 through Base 36. The source number must be in the range of a Long data type which is from -2,147,483,648 to 2,147,483,647.

Just in case some of you do not understand what base means in this context, we will give a short description. Our numerical system is called base 10 because there are 10 digits that are possible (0, 1, 2, 3, 4, 5, 6, 7, 8, 9). Base 2 consists of only 0 and 1. Base 16 would be the numbers from 0 to 9 and the letters from A to F with A=10, B=11, etc. These are the most common bases that are used in the programming world. Some older systems use octal which is base 8 or the numbers 0 through 7.

Function AnyBase(sValueToConvert As String, iBaseIn As Integer, iBaseOut As Integer) As String

Parameters

Returns

Error 51 if the value in sValueToConvert does not match the base supplied in iBaseIn. If successful it will return a string that represents the sValueToConvert in base iBaseOut.

Sample Usage

    MsgBox AnyBase("FFCA", 16, 10)
    ' Will print 65482 in a message box
    MsgBox AnyBase("FFCA", 16, 2)
    ' Will print 1111111111001010 in a message box
    MsgBox AnyBase("74292", 10, 16)
    ' Will print 12234 in a message box

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address: http://www.codeoftheweek.com/membersonly/bi/0069.html


This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945
Email: info@codeoftheweek.com
Web: http://www.codeoftheweek.com