Presented by Redjkibersama.blogspot.com - Silahkan download sepuasnya, dijamin gratis..tis..tis... !

Friday, April 8, 2011

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

Source code berikut berguna untuk membantu konversi bilangan desimal ke biner dan juga sebaliknya, dari biner ke desimal. Mendukung sampai 2147483647 bilangan.
Buat 1 project dengan :
1 Form
2 CommandButton
2 TextBox
Source code :
01Private Sub Command1_Click()
02Dim Decimal1 As Long
03Dim Binary1 As String
04Dim Binary2 As String
05On Error GoTo ErrEnd
06Binary1 = ""
07Binary2 = ""
08Decimal1 = Text1.Text
09Do Until Decimal1 = 0
10If Deelbaar2(Decimal1) = False Then
11Binary2 = Binary2 & 1
12ElseIf Deelbaar2(Decimal1) = True Then
13Binary2 = Binary2 & 0
14End If
15Decimal1 = Int(Decimal1 / 2)
16Loop
17For a = Len(Binary2) To 1 Step -1
18Binary1 = Binary1 & Mid(Binary2, a, 1)
19Next a
20Text2.Text = Binary1
21Exit Sub
22ErrEnd:
23Alert = MsgBox("Error " & Err.Number & ": " & Err.Description, vbCritical)
24End Sub
25 
26Private Sub Command2_Click()
27On Error GoTo ErrEnd
28For a = 1 To Len(Text2.Text)
29If Mid(Text2.Text, a, 1) <> "0" And Mid(Text2.Text, a, 1) <> "1" Then
30MsgBox "Wrong binary number!"
31Exit Sub
32End If
33Next a
34Dim Binary1 As String
35Dim Decimal1 As Long
36Decimal1 = 0
37Binary1 = Text2.Text
38For a = 1 To Len(Binary1)
39Decimal1 = Decimal1 * 2 + Mid(Binary1, a, 1)
40Next a
41Text1.Text = Decimal1
42Exit Sub
43ErrEnd:
44Alert = MsgBox("Error " & Err.Number & ": " & Err.Description, vbCritical)
45End Sub
46 
47Private Function Deelbaar2(Getal As Long) As Boolean
48If Getal = Int(Getal / 2) * 2 Then
49Deelbaar2 = True
50ElseIf Getal <> Int(Getal / 2) * 2 Then
51Deelbaar2 = False
52End If
53End Function
Download Source Code
Mirror link :

No comments:

Daftar Isi