decimal to binary

Reply

Join Date: Mar 2009
Posts: 3
Reputation: sunderthomas is an unknown quantity at this point 
Solved Threads: 0
sunderthomas sunderthomas is offline Offline
Newbie Poster

decimal to binary

 
0
  #1
Jun 9th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 509
Reputation: selvaganapathy is an unknown quantity at this point 
Solved Threads: 88
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro

Re: decimal to binary

 
0
  #2
Jun 10th, 2009
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
  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.
KSG
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: sunderthomas is an unknown quantity at this point 
Solved Threads: 0
sunderthomas sunderthomas is offline Offline
Newbie Poster

Re: decimal to binary

 
0
  #3
Jun 15th, 2009
thanks you prob solved...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC