'********************************************************************

'*

'* Function Dec2BaseX

'*

'*   Author: NetworkAdminKB.com

'*  Created: 2006-06-09

'* Modified: 2006-06-09

'*

'* Purpose: Convert a Base 10 Decimal number to a Base X String.

'*            1) It correctly returns the Negative Base X number and not

'*                 its Complement.

'*            2) It overcomes the VBScript Math (HEX, OCT) Function

'*                 limitation on numbers larger/smaller than

'*                 +/- 2,147,483,647

'*            3) The limitation on this function is:

'*                 +/- 9,007,199,254,740,991 (or 0x1FFFFFFFFFFFFF)

'*            4) The maximum Base X number supported is 36,

'*                 the minimum Base X numer supported is 2.

'*

'* Input:   numAny   A whole number of any size.

'*          numBase     A number between 2 and 36

'*

'* Output:  The Base X String that represents the number.

'*

'********************************************************************

Function Dec2BaseX(ByVal numAny, ByVal numBase)

  'Version 1.0 2006-06-09

  Dim Sign, strBase

 

  Const maxNum = 9007199254740991

  Const BaseChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

 

  If numBase > 36 Or numBase < 2 Then

    Wscript.Echo "Dec2BaseX Error: This function only supports Base X " & _

                 "numbers between 2 and 36."

    Dec2BaseX = Empty

    Exit Function

  Else

    strBase = Mid(BaseChars, 1, numBase)

  End If 'numBase > 36 Or numBase < 2

 

  Sign = Sgn(numAny)

  numAny = Fix(Abs(CDbl(numAny)))

  If numAny > CDbl(maxNum) Then

    Wscript.Echo "Dec2BaseX Error: Unable to return Base X values for " & _

                 "numbers greater/less than +/- 9,007,199,254,740,991"

    Dec2BaseX = Empty

    Exit Function

  End If 'numAny > maxNum

  If numAny = 0 Then

    Dec2BaseX = "0"

    Exit Function

  End If

  While numAny > 0

    Dec2BaseX = Mid(strBase, 1 + (numAny - numBase * _

                Fix(numAny / numBase)), 1) & Dec2BaseX

    numAny = Fix(numAny/numBase)

  WEnd

  If Sign = -1 Then Dec2BaseX = "-" & Dec2BaseX

End Function 'Dec2BaseX

Article ID: 384, Created On: 9/25/2011, Modified: 9/25/2011