943,733 Members | Top Members by Rank

Ad:
Jun 9th, 2009
0

decimal to binary

Expand Post »
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Dim a As Integer, i As Integer, b(20) As Integer, c As Integer, d As Integer
  2.  
  3. i = 0
  4. a = TextBox1.Text
  5. While a > 1
  6. b(i) = a Mod 2
  7. a = a / 2
  8. i = i + 1
  9. End While
  10.  
  11. b(i) = a
  12.  
  13. While i >= 0
  14. c = c & b(i)
  15. i = i - 1
  16. End While
  17. Label5.Text = c
  18. 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..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunderthomas is offline Offline
3 posts
since Mar 2009
Jun 10th, 2009
0

Re: decimal to binary

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
VB Syntax (Toggle Plain Text)
  1. Dim a as Integer
  2.  
  3. a = 3 / 2
  4. MsgBox a ' Gives 2
  5.  
  6. a = 3 \ 2
  7. MsgBox a ' Gives 1
3 / 2 gives 1.5 and it will be converted to 2
Last edited by selvaganapathy; Jun 10th, 2009 at 2:00 pm.
Reputation Points: 44
Solved Threads: 101
Posting Pro
selvaganapathy is offline Offline
547 posts
since Feb 2008
Jun 15th, 2009
0

Re: decimal to binary

thanks you prob solved...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sunderthomas is offline Offline
3 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Deleting Record from Listbox and Sorting a list box
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Listview issue





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC