# Base Number Conversion Routines

Friday Jan 30th 2004 by John Pirkey
Share:

Learn to convert decimal numbers to any base system (up to base 36) and back again. (Added: 3-May-2000)

Here are two routines that will take any decimal number and convert it to any number base between 2 and 36 and vice-versa. The first routine, ConvertDecToBaseN, takes the decimal number and the base to convert it to as parameters. If no base is supplied, then base 16 (hexadecmial) is assumed. The original author of this routine is Peter Rodriguez. I got his code from www.devx.com - it was a "Tip of the Day". In his tip, he encouraged the reader to derive a partner function that would bring his converted number back to base 10. So I accepted his challenge and wrote the second routine, ConvertBaseNToDec, which is modeled after his routine and takes the converted number and converted number's base as parameters and returns the decimal equivalent. If no base is supplied, then base 16 is assumed.

Note: these functions only work with whole numbers. They do not convert rational or irrational numbers correctly at this point.

The code comes with a sample project showing how the routines should be used.

Download Zipped Project File (3k)

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved