Convert a Decimal Number to Base N

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:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing