KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q43567: SUBprogram to Convert Integer to a String in Binary Notation

Article: Q43567
Product(s): See article
Version(s): 3.00 4.00 4.00b 4.50
Operating System(s): MS-DOS
Keyword(s): ENDUSER | SR# S890412-99 B_BasicCom | mspl13_basic
Last Modified: 14-DEC-1989

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:

DEFINT A-Z
'__________________________________________________________________
'
'    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$
                ELSE
                        bin$ = "0" + bin$
                END IF
                 temp% = temp% \ 2
        NEXT

END SUB

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.