-
I am trying to make a program that will convert a Denary number to binary. I want the user to type the denary number in the text box and then press a command button to show the binary number in a lable. Please can somone help me, as i'm quite new to VB and have to make this program today.
Thanks
Mike
-
Sorry.. but what's Denary?
Do you mean Decimal? (with to-the-powers with 10?)
In that case, Yonatan got a short way to do that:
Code:
Function Bin(ByVal lNum As Long) As String
Do
Bin = Chr(48 + (lNum And 1)) & Bin
lNum = lNum \ 2
Loop Until lNum = 0
End Function
and I made a longer one :)
Code:
Private Function MakeBin()
Static bin As String, asc As Long
bin = ""
asc = Val(Text1.Text)
Do
'If 0 Then Exit the loop
If asc = 0 Then Exit Do
'Check if even
If asc Mod 2 = 0 Then
'If even then add 0 to the binair number
bin = "0" & bin
'and divide ascii by 2
asc = asc / 2
Else
'If odd then add 1 to the binair number
bin = "1" & bin
'Subtract 1 and divide by 2
asc = (asc - 1) / 2
End If
Loop
MakeBin = bin
End Function
Have funnn
[Edited by Jop on 11-08-2000 at 11:44 AM]
-
Oh sorry
Any whole number. And if it is not too hard decimals as well.
Thanks
Mike
-
Great
I got it working.
Thanks
Mike