Skip to main content
NetworkAdminKB Logo

VBScript Library

Go Search
NetworkAdminKB.com
Knowledge Base
VBScript Library
Utilities
  
NetworkAdminKB.com > VBScript Library > Knowledge Base > Components > Math > Dec2Oct  

Dec2Oct

Article Content

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

'*

'* Function Dec2Oct

'*

'*   Author: NetworkAdminKB.com

'*  Created: 2004-12-04

'* Modified: 2004-12-04

'*

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

'*            replaces the normal VBScript OCT Function and has the

'*            following benefits.

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

'*                 its Complement as the VBScript HEX function does.

'*            2) It overcomes the VBScript HEX Functions limitation

'*                on numbers larger/small 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 OCT string that represents the number.

'*

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

Function Dec2Oct(ByVal numAny)

  'Version 1.0 2004-12-04

  Dim Sign

  Const maxNum = 9007199254740991

  Const OctChars = "01234567"

  Sign = Sgn(numAny)

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

  If numAny > CDbl(maxNum) Then

    Wscript.Echo "Dec2Oct Error: Unable to return OCT values for numbers " & _

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

    Dec2Oct = Empty

    Exit Function

  End If 'numAny > maxNum

  If numAny = 0 Then

    Dec2Oct = "0"

    Exit Function

  End If

  While numAny > 0

    Dec2Oct = Mid(OctChars, 1 + (numAny - 8 * Fix(numAny / 8)), 1) & Dec2Oct

    numAny = Fix(numAny/8)

  WEnd

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

End Function 'Dec2Oct

Keywords
Math Conversion 
Related Articles
 
Last modified at 1/12/2009 8:12 PM  by TEMPLATE\kbadmin 
 
 NetworkAdminKB.com
 Copyright © 2008 NetworkAdminKB.com, All rights reserved. Terms of Use | Contact US