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.
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 firstname.lastname@example.org.
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
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.
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
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