Jumat, 20 Mei 2016

Konversi Bilangan dengan Pemrograman Visual Basic 6.0

 



Konversi bilangan adalah proses mengubah bentuk bilangan satu ke bentuk bilangan lain yang memiliki nilai yang sama. Misal: nilai bilangan desimal 12 memiliki nilai yang sama dengan bilangan octal 15; Nilai bilangan biner 10100 memiliki nilai yang sama dengan 24 dalam octal dan seterusnya.

Konversi bilangan biner, octal atau hexadesimal menjadi bilangan desimal.
Konversi dari bilangan biner, octal atau hexa menjadi bilangan desimal memiliki konsep yang sama.Konsepnya adalah bilangan tersebut dikalikan basis bilangannya yang dipangkatkan 0,1,2 dst dimulai dari kanan. Untuk lebih jelasnya silakan lihat contoh konversi bilangan di bawah ini;
Konversi bilangan octal ke desimal.
Cara mengkonversi bilangan octal ke desimal adalah dengan mengalikan satu-satu bilangan dengan 8 (basis octal) pangkat 0 atau 1 atau 2 dst dimulai dari bilangan paling kanan. Kemudian hasilnya dijumlahkan. Misal, 137(octal) = (7x80) + (3x81) + (1x82) = 7+24+64 = 95(desimal).

Konversi bilangan biner ke desimal.
Cara mengkonversi bilangan biner ke desimal adalah dengan mengalikan satu-satu bilangan dengan 2 (basis biner) pangkat 0 atau 1 atau 2 dst dimulai dari bilangan paling kanan. Kemudian hasilnya dijumlahkan. Misal, 11001(biner) = (1x20) + (0x21) + (0x22) + (1x2) + (1x22) = 1+0+0+8+16 = 25(desimal).

Konversi bilangan hexadesimal ke desimal.
Cara mengkonversi bilangan biner ke desimal adalah dengan mengalikan satu-satu bilangan dengan 16 (basis hexa) pangkat 0 atau 1 atau 2 dst dimulai dari bilangan paling kanan. Kemudian hasilnya dijumlahkan. Misal, 79AF(hexa) = (Fx20) + (9x21) + (Ax22) = 15+144+2560+28672 = 31391(desimal).

Konversi bilangan desimal menjadi bilangan biner, octal atau hexadesimal.
Konversi dari bilangan desimal menjadi biner, octal atau hexadesimal juga memiliki konse yang sama. Konsepnya bilangan desimal harus dibagi dengan basis bilangan tujuan, hasilnya dibulatkan kebawah dan sisa hasil baginya (remainder) disimpan. Ini dilakukan terus menerus hingga hasil bagi < basis bilangan tujuan. Sisa bagi ini kemudian diurutkan dari yang paling akhir hingga yang paling awal dan inilah yang merupakan hasil konversi bilangan tersebut. Untuk lebih jelasnya lihat pada contoh berikut;
Konversi bilangan desimal ke biner.
Cara konversi bilangan desimal ke biner adalah dengan membagi bilangan desimal dengan 2 dan menyimpan sisa bagi per seitap pembagian terus hingga hasil baginya < 2. Hasil konversi adalah urutan sisa bagi dari yang paling akhir hingga paling awal. Contoh:

125(desimal) = .... (biner)
125/2 = 62 sisa bagi 1
62/2= 31    sisa bagi 0
31/2=15     sisa bagi 1
15/2=7       sisa bagi 1
7/2=3         sisa bagi 1
3/2=1         sisa bagi 1
hasil konversi: 1111101

Konversi bilangan desimal ke octal.
Cara konversi bilangan desimal ke octal adalah dengan membagi bilangan desimal dengan 8 dan menyimpan sisa bagi per seitap pembagian terus hingga hasil baginya < 8. Hasil konversi adalah urutan sisa bagi dari yang paling akhir hingga paling awal. 

Konversi bilangan desimal ke hexadesimal.
Cara konversi bilangan desimal ke octal adalah dengan membagi bilangan desimal dengan 16 dan menyimpan sisa bagi per seitap pembagian terus hingga hasil baginya < 16. Hasil konversi adalah urutan sisa bagi dari yang paling akhir hingga paling awal. Apabila sisa bagi diatas 9 maka angkanya diubah, untuk nilai 10 angkanya A, nilai 11 angkanya B, nilai 12 angkanya C, nilai 13 angkanya D, nilai 14 angkanya E, nilai 15 angkanya F.
 
Listing Proram :
1.       Module Konversi Bilangan
Public a, b As Integer
Public Function BinToDes(ByVal NBiner As String) As Long
  Dim a         As Integer
  Dim b         As Long
  Dim Nilai    As Long
  On Error GoTo ErrorHandler
  b = 1
  For a = Len(NBiner) To 1 Step -1
    If Mid(NBiner, a, 1) = "1" Then Nilai = Nilai + b
    b = b * 2
  Next
  BinToDes = Nilai
  Exit Function
ErrorHandler:
  BinToDes = 0
End Function


Public Function DesToBin(ByVal NDesimal As Long) As String
  Dim C        As Byte
  Dim D        As Long
  Dim Nilai    As String
  On Error GoTo ErrorHandler
  D = (2 ^ 31) - 1
  While D > 0
    If NDesimal - D >= 0 Then
      NDesimal = NDesimal - D
      Nilai = Nilai & "1"
    Else
      If Val(Nilai) > 0 Then Nilai = Nilai & "0"
    End If
    D = D / 2
  Wend
  DesToBin = Nilai
  Exit Function
ErrorHandler:
  DesToBin = 0
End Function

Public Function DesToHex(ByVal NDesimal As Long) As String
  DesToHex = Hex(NDesimal)
End Function
Public Function DesToOk(ByVal NDesimal As Long) As String
  DesToOk = Oct(NDesimal)
End Function

Public Function OkToDes(ByVal NOktal As String) As Long
  Dim G          As Integer
  Dim H          As Long
  Dim Nilai      As Long
  On Error GoTo ErrorHandler
  For G = Len(NOktal) To 1 Step -1
    Nilai = Nilai + (8 ^ H) * CInt(Mid(NOktal, G, 1))
    H = H + 1
  Next G
  OkToDes = Nilai
  Exit Function
ErrorHandler:
  OkToDes = 0
End Function

Public Function BinToOk(ByVal bin As Long) As String
 BinToOk = DesToOk(BinToDes(bin))
End Function

Public Function BinToHex(ByVal NBiner As Long) As String
 BinToHex = DesToHex(BinToDes(NBiner))
End Function

Public Function OkToBin(ByVal NOktal As Double) As String
    OkToBin = DesToBin(OkToDes(NOktal))
End Function
Public Function OkToHex(ByVal NOktal As Double) As String
    OkToHex = DesToHex(OkToDes(NOktal))
End Function

Public Function HexToBin(ByVal NHexa As String) As String
    HexToBin = DesToBin(HexToDes(NHexa))
End Function
Public Function HexToOk(ByVal NHexa As String) As Double
    HexToOk = DesToOk(HexToDes(NHexa))
End Function



2.       Program Utama Konversi Bilangan

Option Explicit

Private Sub command2_Click()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text8.Text = ""
End Sub

Private Sub command3_Click()
End
End Sub
Private Sub command1_Click()
If Len(Trim(Text1.Text)) = 0 Then
  MsgBox "Nilai Bilangan Desimal mohon di Masukkan"
  Text1.SetFocus
Else
        If Option1.Value And Option6.Value Then Text2.Text = DesToBin(Text1.Text)
        If Option1.Value And Option7.Value Then Text2.Text = DesToOk(Text1.Text)
        If Option1.Value And Option7.Value Then Text8.Text = OkToBin(Text2.Text)
        If Option1.Value And Option8.Value Then Text2.Text = DesToHex(Text1.Text)
        If Option1.Value And Option8.Value Then Text8.Text = HexToBin(Text2.Text)
        'konversi 1 to lsb dan msb dan ke biner
        If Option6.Value = True Then
          If Len(Trim(Text2.Text)) = 1 Then
              Text2.Text = "0000000" & Text2.Text
              Text3.Text = Right(Text2.Text, 7)
              Text4.Text = Left(Text2.Text, 7)
              Label5.Caption = Left(Text2.Text, 1)
              Label6.Caption = Right(Text2.Text, 1)
          ElseIf Len(Trim(Text2.Text)) = 2 Then
              Text2.Text = "000000" & Text2.Text
              Text3.Text = Right(Text2.Text, 7)
              Text4.Text = Left(Text2.Text, 7)
              Label5.Caption = Left(Text2.Text, 1)
              Label6.Caption = Right(Text2.Text, 1)
          ElseIf Len(Trim(Text2.Text)) = 3 Then
              Text2.Text = "00000" & Text2.Text
              Text3.Text = Right(Text2.Text, 7)
              Text4.Text = Left(Text2.Text, 7)
              Label5.Caption = Left(Text2.Text, 1)
              Label6.Caption = Right(Text2.Text, 1)
          ElseIf Len(Trim(Text2.Text)) = 4 Then
              Text2.Text = "0000" & Text2.Text
              Text3.Text = Right(Text2.Text, 7)
              Text4.Text = Left(Text2.Text, 7)
              Label5.Caption = Left(Text2.Text, 1)
              Label6.Caption = Right(Text2.Text, 1)
          ElseIf Len(Trim(Text2.Text)) = 5 Then
              Text2.Text = "000" & Text2.Text
              Text3.Text = Right(Text2.Text, 7)
              Text4.Text = Left(Text2.Text, 7)
              Label5.Caption = Left(Text2.Text, 1)
              Label6.Caption = Right(Text2.Text, 1)
          ElseIf Len(Trim(Text2.Text)) = 6 Then
              Text2.Text = "00" & Text2.Text
              Text3.Text = Right(Text2.Text, 7)
              Text4.Text = Left(Text2.Text, 7)
              Label5.Caption = Left(Text2.Text, 1)
              Label6.Caption = Right(Text2.Text, 1)
          ElseIf Len(Trim(Text2.Text)) = 7 Then
              Text2.Text = "0" & Text2.Text
              Text3.Text = Right(Text2.Text, 7)
              Text4.Text = Left(Text2.Text, 7)
              Label5.Caption = Left(Text2.Text, 1)
              Label6.Caption = Right(Text2.Text, 1)
          Else
              Text2.Text = Text2.Text
              a = Len(Trim(Text2))
              b = a - 1
              Text3.Text = Right(Text2.Text, b)
              Text4.Text = Left(Text2.Text, b)
              Label5.Caption = Left(Text2.Text, 1)
              Label6.Caption = Right(Text2.Text, 1)
          End If
        'Oktal ke LSB dan MSB
        ElseIf Option7.Value = True Then
          If Len(Trim(Text8.Text)) = 1 Then
              Text8.Text = "0000000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 2 Then
              Text8.Text = "000000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 3 Then
              Text8.Text = "00000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 4 Then
              Text8.Text = "0000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 5 Then
              Text8.Text = "000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 6 Then
              Text8.Text = "00" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 7 Then
              Text8.Text = "0" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          Else
              Text8.Text = Text8.Text
              a = Len(Trim(Text8.Text))
              b = a - 1
              Text3.Text = Right(Text8.Text, b)
              Text4.Text = Left(Text8.Text, b)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          End If
        ElseIf Option8.Value = True Then
          If Len(Trim(Text8.Text)) = 1 Then
              Text8.Text = "0000000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 2 Then
              Text8.Text = "000000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 3 Then
              Text8.Text = "00000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 4 Then
              Text8.Text = "0000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 5 Then
              Text8.Text = "000" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 6 Then
              Text8.Text = "00" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          ElseIf Len(Trim(Text8.Text)) = 7 Then
              Text8.Text = "0" & Text8.Text
              Text3.Text = Right(Text8.Text, 7)
              Text4.Text = Left(Text8.Text, 7)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          Else
              Text8.Text = Text8.Text
              a = Len(Trim(Text8.Text))
              b = a - 1
              Text3.Text = Right(Text8.Text, b)
              Text4.Text = Left(Text8.Text, b)
              Label5.Caption = Left(Text8.Text, 1)
              Label6.Caption = Right(Text8.Text, 1)
          End If
        Else
                Text2.Text = Text2.Text
        End If
        With Text1
          .SelStart = 0
          .SelLength = Len(Text1.Text)
        End With
End If
End Sub

Private Sub Form_Load()
Move (Screen.Width - Me.Width) / 2, (Screen.Height - Me.Height) / 2
  Text1.Text = ""
  Text2.Text = ""
  Text3.Text = ""
  Text4.Text = ""
  Text5.Text = ""
  Text6.Text = ""
Option1.Value = True
End Sub

Private Sub Option10_Click()
    Option11.Value = False
    Option12.Value = False
    Text6.Text = Text3.Text
    Text5.Text = Text4.Text
End Sub

Private Sub Option11_Click()
    Option11.Value = True
    Option10.Value = False
    Option12.Value = False
    Text6.Text = BinToOk(Text3.Text)
    Text5.Text = BinToOk(Text4.Text)
End Sub

Private Sub Option12_Click()
Option12.Value = True
    Option10.Value = False
    Option11.Value = False
    Text6.Text = BinToHex(Text3.Text)
    Text5.Text = BinToHex(Text4.Text)
End Sub

untuk Program Lebih lengkap Download dibawah ini 
Konversi Bilangan dengan VB

Semoga bermanfaat.
 

Tidak ada komentar:
Write komentar


Follow us on FB

Featured Video

Pencarian..