Base Number Conversion Routines

screen-shot

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)



Comments

  • http://www.codeguru.com/vb/articles/2137.shtml

    Posted by Legacy on 12/27/2001 12:00am

    Originally posted by: CHris Majagah

    Very very nice neat code for converting numbers to different base numbers

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

  • As a development and deployment platform, RHEL offers an efficient, scalable, and robust operating environment with certified security and flexible deployment options in physical and virtualized environments. To assess and quantify the business benefits of RHEL, IDC recently conducted in-depth interviews with IT staff members of 21 companies using RHEL servers. The organizations represent a broad range of industries and have an average of 22,700 employees. RHEL servers accounted for 23% of the servers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds