You are currently offline, waiting for your internet to reconnect

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

This article was previously published under Q109260
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 itsequivalent 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. Negativenumbers are converted into the 32-digit, twos-complement binary format usedby long integers in Basic.
In decimal numbers (base-ten numbers), every decimal place is a powerof 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 digitscan 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"         Else            bin = bin + "0"         End If      Next      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 internaltwos-complement binary format used by Basic. In that format, the left-mostbinary digit (the thirty-second digit in a long integer) will always be 1for a negative number and 0 for a positive number.
Decimal Value    Binary Value-----------------------------0                0000000000000000000000000000000021               000000000000000000000000000101011024             0000000000000000000001000000000032767            0000000000000000011111111111111132768            0000000000000000100000000000000065536            0000000000000001000000000000000016777216         00000001000000000000000000000000999999999        00111011100110101100100111111111-1               11111111111111111111111111111111-3               11111111111111111111111111111101				
2.00 3.00

Article ID: 109260 - Last Review: 12/09/2003 14:34:34 - Revision: 2.0

  • 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
  • KB109260