How to Convert a Decimal Number to a Binary Number in a String

Retired KB Content Disclaimer

This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.


The following sample program shows how to convert a decimal number into its equivalent binary representation stored in a string.

This program accepts a nine-digit positive decimal number and returns a 32- character string that represents the number in binary notation. Negative numbers are converted into the 32-digit, twos-complement binary format used by long integers in Basic.

More Information

In decimal numbers (base-ten numbers), every decimal place is a power of 10. Decimal digits can have values from zero to nine. In binary numbers (base-two numbers), every decimal place is a power of two. Binary digits can only have values of 0 or 1.

Sample Program

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add two text boxes to Form1. Make each text box more than 32 characters wide.
  3. Double-click the Text1 text box to open its code window. Choose the Change event from the Proc box. Add the following code to the Text1 Change event:
       Sub Text1_Change ()

    Dim i As Long, x As Long, bin As String
    Const maxpower = 30 ' Maximum number of binary digits supported.
    text1.MaxLength = 9 ' Maximum number of decimal digits allowed.
    text2.Enabled = False ' Prevent typing in second text box.
    bin = "" 'Build the desired binary number in this string, bin.
    x = Val(text1.Text) 'Convert decimal string in text1 to long integer

    If x > 2 ^ maxpower Then
    MsgBox "Number must be no larger than " & Str$(2 ^ maxpower)
    text2.Text = ""
    Exit Sub
    End If

    ' Here is the heart of the conversion from decimal to binary:

    ' Negative numbers have "1" in the 32nd left-most digit:
    If x < 0 Then bin = bin + "1" Else bin = bin + "0"

    For i = maxpower To 0 Step -1
    If x And (2 ^ i) Then ' Use the logical "AND" operator.
    bin = bin + "1"
    bin = bin + "0"
    End If
    text2.Text = bin ' The bin string contains the binary number.

    End Sub
  4. Start the program, or press the F5 key. Enter decimal numbers into the first text box. The binary equivalent number displays in the second text box.
NOTE: This program converts negative decimal numbers into the internal twos-complement binary format used by Basic. In that format, the left-most binary digit (the thirty-second digit in a long integer) will always be 1 for a negative number and 0 for a positive number.

Decimal Value Binary Value
0 00000000000000000000000000000000
21 00000000000000000000000000010101
1024 00000000000000000000010000000000
32767 00000000000000000111111111111111
32768 00000000000000001000000000000000
65536 00000000000000010000000000000000
16777216 00000001000000000000000000000000
999999999 00111011100110101100100111111111
-1 11111111111111111111111111111111
-3 11111111111111111111111111111101

Article ID: 109260 - Last Review: Dec 9, 2003 - Revision: 1