954,551 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

decimal to binary

Dim a As Integer, i As Integer, b(20) As Integer, c As Integer, d As Integer

        i = 0
        a = TextBox1.Text
        While a > 1
            b(i) = a Mod 2
            a = a / 2
            i = i + 1
        End While
        
  b(i) = a

        While i >= 0
            c = c & b(i)
            i = i - 1
        End While
        Label5.Text = c
        Label5.Visible = True


hey guys i wrote the above prog to convert decimal to binary the result i get is wrong for the last digit..


eg when converting 100 i get 10100100 instead of 1100100

thanks in advance..

sunderthomas
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

HI

Use \ operator instead of / operator, because / operator in vb gives floating result and \ operator gives integer result. so floating value will be rounded to integer.

For example

Dim a as Integer

   a = 3 / 2      
   MsgBox a     ' Gives 2

   a = 3 \ 2
   MsgBox a     ' Gives 1

3 / 2 gives 1.5 and it will be converted to 2

selvaganapathy
Posting Pro
547 posts since Feb 2008
Reputation Points: 44
Solved Threads: 100
 

thanks you prob solved...

sunderthomas
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You