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

'*

'* Function Dec2Hex

'*

'*   Author: NetworkAdminKB.com

'*  Created: 2004-12-04

'* Modified: 2004-12-04

'*

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

'*            replaces the normal VBScript HEX Function and has the

'*            following benefits.

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

'*                 its Complement as the VBScript HEX function does.

'*            2) It overcomes the VBScript HEX Functions limitation

'*                on numbers larger/smaller than +/- 2,147,483,647

'*            2) The limitation on this function is:

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

'*

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

'*

'* Output:  The HEX string that represents the number.

'*

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

Function Dec2Hex(ByVal numAny)

  'Version 1.0 2004-12-04

  Dim Sign

  Const maxNum = 9007199254740991

  Const HexChars = "0123456789ABCDEF"

  Sign = Sgn(numAny)

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

  If numAny > CDbl(maxNum) Then

    Wscript.Echo "Dec2Hex Error: ", _

                 "numAny must be greater/less than +/- 9,007,199,254,740,991"

    Dec2Hex = Empty

    Exit Function

  End If 'numAny > maxNum

  If numAny = 0 Then

    Dec2Hex = "0"

    Exit Function

  End If

  While numAny > 0

    Dec2Hex = Mid(HexChars, 1 + (numAny - 16 * Fix(numAny / 16)), 1) & Dec2Hex

    numAny = Fix(numAny/16)

  WEnd

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

End Function 'Dec2Hex

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