Microsoft KB Archive/43567

From BetaArchive Wiki
Knowledge Base

SUBprogram to Convert Integer to a String in Binary Notation

Article ID: 43567

Article Last Modified on 11/21/2006


  • Microsoft QuickBasic 3.0
  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS

This article was previously published under Q43567


Below is a QuickBasic SUBprogram that converts an integer to binary notation (represented in a string form). Both an integer and a string variable are passed to the SUBprogram and the result is returned in the string variable. The integer is broken down bit by bit and converted into its hexadecimal equivalent.

This code will execute correctly in Microsoft QuickBasic Versions 3.00, 4.00, 4.00b, and 4.50, in Microsoft Basic Compiler Versions 6.00 and 6.00b, and in Microsoft Basic PDS Version 7.00.

The code example is as follows:

'    IntToBin() takes an INTEGER argument and produces a
'    binary string representation of the INTEGER.
SUB IntToBin (byte%, bin$)         'Add STATIC here to get SUB to
bin$ = ""                          'work in QuickBasic 3.00
temp% = byte%

        FOR i = 0 TO 7
                IF temp% AND 1 THEN
                        bin$ = "1" + bin$
                        bin$ = "0" + bin$
                END IF
                 temp% = temp% \ 2


Additional query words: QuickBas BasicCom

Keywords: KB43567