Convert a Decimal Number to Base N

Here’s a function that converts a decimal number (base 10) to another base number system. Each digit position corresponds to a power of N, where N is a number between 2 and 36. In other words, if a number system’s base is N, then N digits are used to write numbers in that system. For example, the base 2 number system (binary) uses the digits 0 and 1, while the base 20 system uses digits 0 through K.

The ConvertDecToBaseN function accepts a double-value decimal number and a byte-value representing the base number between 2 and 36. By default, the base value used is 16 (hexadecimal). The decimal number is converted to a positive number if it’s negative. This function is useful for representing large numbers as strings, using fewer digit positions. I developed it to help reduce the footprint of several large numbers used in constructing a 16-character unique string ID. (Creating a complementary function to convert a base N number back into a decimal would be a great exercise.)

 Public Function ConvertDecToBaseN(ByVal dValue As Double, _	Optional ByVal byBase As Byte = 16) As String	Const BASENUMBERS As String = _		"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"	Dim sResult As String	Dim dRemainder As Double	On Error GoTo ErrorHandler	sResult = ""	If (byBase < 2) Or (byBase > 36) Then GoTo Done	dValue = Abs(dValue)Do	dRemainder = dValue - (byBase * Int((dValue / byBase)))	sResult = Mid$(BASENUMBERS, dRemainder + 1, 1) & sResult	dValue = Int(dValue / byBase)Loop While (dValue > 0)Done:	ConvertDecToBaseN = sResult	Exit FunctionErrorHandler:	Err.Raise Err.Number, "ConvertDecToBaseN", _		Err.DescriptionEnd FunctionSample usage:ConvertDecToBaseN(999999999999#, 36)'Returns 'CRE66I9R
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS