Solved

Binary convertion

Posted on 1998-07-25
3
237 Views
Last Modified: 2010-05-03
Can anyone tell me how to convert decimal to binary in VB?
0
Comment
Question by:twosons
3 Comments
 
LVL 2

Expert Comment

by:chris_a
ID: 1466672
I assume you mean '7' -> 0000 0111

The usual way to do this is to see if it is odd then divide by 2 in a loop.
0
 
LVL 7

Accepted Solution

by:
kamall earned 50 total points
ID: 1466673
On your form, put 2 text boxes and one command button. Copy and paste the following code in the general-declarations section of the form:

Private Sub Command1_Click()

    DecValue = Val(Text1.Text)
   
    BinValue = ""
   
    Do
    TempValue = DecValue Mod 2
        BinValue = CStr(TempValue) + BinValue
    DecValue = DecValue \ 2
    Loop Until DecValue = 0
   
    'Print
    'Print BinValue
   
    Text2.Text = BinValue

End Sub

Run the project, enter your number in the Text1 box and click the command button. Text2 text box will show the binary value of the number.

Hope that helps.
Regards
0
 

Author Comment

by:twosons
ID: 1466674
Thank you very much "kamall".  I knew the loop structure you have mentioned, but I was hoping to see if there is any other way to convert to binary, like "oct(number), hex(number)" in VB.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question