Decimal To Binary Function Does Not Work

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am using the following sub but i am getting inaccurate output

Public Function Convert(ByVal Base As Integer, ByVal Number As Integer) As Strin

D
If Not Number / Base = 0 The
Convert &= (Number Mod Base).ToStrin
Number = Number / Bas
Els
Return StrReverse(Convert
End I
Loo

End Functio

Does any one know why this is not working

Thanks for any help in advance
 
WStoreyII,
Do you need to use floating division?

I would expect that Integer division would be better.

Remember "/" is floating division, while "\" is integer division.

Or are you referring to the fact your routine drops the leading digit?


Have you considered using System.Convert.ToString(value as Integer, tobase
As Integer) instead?

Have you tried:
Public Function Convert(ByVal Base As Integer, ByVal Number As Integer) As String

Do
If number <> 0 Then
Convert &= (Number Mod Base).ToString
Number = Number \ Base
Else
Return StrReverse(Convert)
End If
Loop

End Function

Note the above returns nothing when the input = 0!

Hope this helps
Jay
 
Back
Top