eHow launches Android app: Get the best of eHow on the go.

How To

How to Convert Decimal to Binary in Visual Basic

Contributor
By Robert Karr
eHow Contributing Writer
(0 Ratings)

By creating a short utility program in Visual Basic 6.0 or Visual Basic Express you can build your own converter to change decimal numbers to binary equivalents. All that is required is to create an interface form with one command to start the process and two functions that carry out the operation. The functions translate an otherwise laborious manual process of working with descending powers of two and subtraction operations to arrive at a final binary number.

Difficulty: Moderately Challenging
Instructions

Things You'll Need:

  • Visual Basic 6.0 or Visual Basic Express
  1. Step 1

    Create an executable program in Visual Basic by opening the program and clicking on "File." Then click on "New Project" and choose the standard EXE type of project.

  2. Step 2

    Place a button on the form by double-clicking that tool in the Toolbox. Scan down the "Properties" pane on the right side and change the caption of the button to "Convert Decimal to Binary."

  3. Step 3

    Double-click on the button that now should read "Convert Decimal to Binary" to open the direct code-writing window. Type in the following commands between the "Private Sub Command1_Click()" and "End Sub" that you see:
    Dim DecimalNbr As Long
    DecimalNbr = InputBox("Enter the decimal number to convert to binary")
    MsgBox ("The binary equivalent is " & BinaryNbr(DecimalNbr))

  4. Step 4

    Skip several lines after the above code and type the following:
    Function ExpUp(ByVal exp As Long) As Long
    Static interim(0 To 31) As Long, n As Integer
    If interim(0) = 0 Then
    interim(0) = 1
    For n = 1 To 30
    interim(n) = interim(n - 1) * 2
    Next
    interim(31) = &H80000000
    End If
    ExpUp = interim(exp)
    End Function

  5. Step 5

    Skip several lines below the above lines in the code and enter this:
    Function BinaryNbr(ByVal calc As Long) As String
    Dim interim As String, exp As Integer
    interim = String$(32, "0")
    Do
    If calc And ExpUp(exp) Then
    Mid$(interim, 32 - exp, 1) = "1"
    calc = calc Xor ExpUp(exp)
    End If
    exp = exp + 1
    Loop While calc
    BinaryNbr = Mid$(interim, 33 - exp)
    End Function

  6. Step 6

    Press "F5" to run the application. Click on the "Enter decimal to binary" button. Enter any decimal number (whole) and press "Return." If you have entered the above code correctly, you will see the binary equivalent.

  7. Step 7

    Save the project and the form with an easily remembered name like "Binary Conversion." Then click "File" and "Make Binary Conversion.exe. Save the result on your desktop for easy access.

Tips & Warnings
  • For permanent use, dress up the application by giving the form a name in the Form Properties pane. Add more text to the form by using a "Label" tool to hold information about how binary conversions are done. Add Labels and TextBox tools to the form to request the decimal number and display the result instead of the simple InputBox and MsgBox in Step Three.
  • Visual Basic, like other computer programming languages, is completely intolerant of typographical errors or missing characters. If this utility does not run correctly, then recheck the code you entered.
Resources

Post a Comment

Post a Comment
  • Have you done this? Click here to let us know.
I Did This

Related Ads

Computers
Alexia Petrakos,

Meet Alexia Petrakos eHow's Computers Expert.

Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics