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

  • With 81% of employees using their phones at work, companies have stopped asking: "Is corporate data leaking from personal devices?" and started asking: "How do we effectively prevent corporate data from leaking from personal devices?" The answer has not been simple. ZixOne raises the bar on BYOD security by not allowing email data to reside on the device. In addition, Zix allows employees to maintain complete control of their personal device, therefore satisfying privacy demands of valued employees and the …

  • Cybercrime is getting big and bigger. 2013 was the year of the Mega Breach with eight top data breaches resulting in the loss of tens of millions of data records. Criminals are always looking for vulnerabilities to exploit. Applications are already becoming a target. Is signing code and apps the answer to protecting our users from malware in applications? Are there any challenges with code signing? In this program we answer those questions and more with two of the industry's leading experts -- Mario de …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds