• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Binary convertion

Can anyone tell me how to convert decimal to binary in VB?
1 Solution
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.
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 = ""
    TempValue = DecValue Mod 2
        BinValue = CStr(TempValue) + BinValue
    DecValue = DecValue \ 2
    Loop Until DecValue = 0
    '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.
twosonsAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now