Microsoft KB Archive/31789

= Converting Between Decimal, Octal, and Binary Numbers =

Article ID: 31789

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic for MS-DOS
 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft BASIC Compiler 6.0b, when used with:
 * Apple Macintosh OS X
 * Microsoft BASIC Interpreter 1.0
 * Microsoft BASIC Interpreter 1.01 for Macintosh
 * Microsoft BASIC Interpreter 2.0
 * Microsoft BASIC Interpreter 2.1 for Macintosh
 * Microsoft BASIC Interpreter 3.0 for Macintosh
 * Microsoft QuickBasic 1.0
 * Microsoft QuickBasic 1.01
 * Microsoft QuickBasic 1.02
 * Microsoft QuickBasic 2.0
 * Microsoft QuickBasic 2.01
 * Microsoft QuickBasic 3.0
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1
 * Microsoft GW-BASIC Interpreter 3.20

-



This article was previously published under Q31789



SUMMARY
The Microsoft Basic code below demonstrates how to perform the following conversions between decimal, octal, and binary numbers:


 * Convert an octal number to a decimal (base 10) number
 * Convert a decimal number to an octal (base 8) number
 * Convert a decimal number to a binary (base 2) number



MORE INFORMATION
The following code example will perform the conversions: ' 1. Octal string to decimal Number: INPUT &quot;INPUT Octal number:&quot;; octal$ PRINT VAL(&quot;&O&quot; + octal$)

' 2. Decimal Number to octal string:

INPUT &quot;INPUT Decimal number:&quot;; Decimal PRINT OCT$(Decimal)

' 3. Decimal Number to Binary string: Bin$ = &quot;&quot; INPUT &quot;INPUT Decimal number:&quot;; Decimal FOR i = 14 TO 0 STEP -1          ' Positive numbers only pow2 = 2 ^ i IF Decimal >= pow2 THEN Decimal = Decimal - pow2: Bin$ = Bin$ + &quot;1&quot; ELSE Bin$ = Bin$ + &quot;0&quot; END IF NEXT i PRINT Bin$ The above information applies to most Microsoft Basic products, including the following Basics:


 * Microsoft Visual Basic version 1.0 for MS-DOS
 * Microsoft Visual Basic versions 1.0, 2.0, 3.0 for Microsoft Windows. NOTE: You will need to replace the INPUT statement in the above code with a feature that is supported in this product, such as a text box used with a Click event procedure.
 * Microsoft Basic Compiler version 1.0 for the Apple Macintosh
 * Microsoft Basic Interpreter versions 1.0, 1.01, 2.0, 2.1, and 3.0 for the Apple Macintosh
 * Microsoft QuickBasic Compiler versions 1.0, 1.01, 1.02, 2.0, 2.01, 3.0, 4.0, 4.0b, and 4.5 for the IBM PC
 * Microsoft Basic Compiler versions 5.35 and 5.36 for MS-DOS
 * Microsoft Basic Compiler version 6.0 for MS-DOS and MS OS/2
 * Microsoft Basic PDS versions 7.0 and 7.1 for MS-DOS and MS OS/2
 * Microsoft GW-Basic Interpreter version 3.2

Additional query words: VBmsdos QuickBas BasicCom 1.00 2.10 3.00 3.20 4.00 4.00b 4.50 6.00 6.00b 7.00 7.10

Keywords: KB31789

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.