IP

Blog Archive

Followers

Senin, 27 Februari 2012

Script VB Untuk Konversi Bilangan Desimal Ke Biner dan Biner Ke Desimal

Buat 1 project dengan :
1 Form
2 CommandButton
2 TextBox

Source code :

Private Sub Command1_Click()
Dim Decimal1 As Long
Dim Binary1 As String
Dim Binary2 As String
On Error GoTo ErrEnd
Binary1 = ""
Binary2 = ""
Decimal1 = Text1.Text
Do Until Decimal1 = 0
If Deelbaar2(Decimal1) = False Then
Binary2 = Binary2 & 1
ElseIf Deelbaar2(Decimal1) = True Then
Binary2 = Binary2 & 0
End If
Decimal1 = Int(Decimal1 / 2)
Loop
For a = Len(Binary2) To 1 Step -1
Binary1 = Binary1 & Mid(Binary2, a, 1)
Next a
Text2.Text = Binary1
Exit Sub
ErrEnd:
Alert = MsgBox("Error " & Err.Number & ": " & Err.Description, vbCritical)
End Sub

Private Sub Command2_Click()
On Error GoTo ErrEnd
For a = 1 To Len(Text2.Text)
If Mid(Text2.Text, a, 1) <> "0" And Mid(Text2.Text, a, 1) <> "1" Then
MsgBox "Wrong binary number!"
Exit Sub
End If
Next a
Dim Binary1 As String
Dim Decimal1 As Long
Decimal1 = 0
Binary1 = Text2.Text
For a = 1 To Len(Binary1)
Decimal1 = Decimal1 * 2 + Mid(Binary1, a, 1)
Next a
Text1.Text = Decimal1
Exit Sub
ErrEnd:
Alert = MsgBox("Error " & Err.Number & ": " & Err.Description, vbCritical)
End Sub

Private Function Deelbaar2(Getal As Long) As Boolean
If Getal = Int(Getal / 2) * 2 Then
Deelbaar2 = True
ElseIf Getal <> Int(Getal / 2) * 2 Then
Deelbaar2 = False
End If
End Function

http://www.planet-source-code.com

0 komentar:

Like This Yo...!!

Share
free counters

Di kunjungin

Website counter

Translate this Blog

Indonesian Blogger

Banner iskaruji dot com

Enter your email address:

Delivered by FeedBurner

Chat Box