Tuesday, January 3, 2012

SIMPLE DECIMAL TO BINARY CONVERTER (VB.NET)

 "Victor.
'This piece of code  converts decimal whole number to binary number

#Region "Imports"
import system.windows.forms.form
#End region
public Class Form1
    Inherits System.Windows.Forms.Form

#region "Windows Form Generated Codes"
public sub new()
mybase.new()
InitializeComponents()
end sub

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(69, 44)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(132, 20)
        Me.TextBox1.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(69, 90)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(132, 52)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Convert"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(69, 163)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(132, 20)
        Me.TextBox2.TabIndex = 2
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(271, 224)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
#End Region

Private x As Integer
    Private y As Integer
    Private Z As Double
    Private NewRtbx As New RichTextBox
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim intX As Integer
            Me.NewRtbx.Clear()
            Me.TextBox2.Clear()
            x = Double.Parse(Me.TextBox1.Text)
            Do
                y = x Mod 2 'gets the remender
                Z = x / 2 'gets the divident
                If y = 0 Then
                    NewRtbx.AppendText(0) 'adds bit 0
                ElseIf y > 0 Then
                    NewRtbx.AppendText(1) 'adds bit 1
                End If
                If Z.ToString.Contains(".5") Then 'remove decimals
                    Z -= 0.5
                    Z = Math.Round(Z, 0)
                End If
                If Z = Math.Round(Z, 0) Then 'gets integer
                    x = Nothing
                    x = Z
                End If
            Loop Until x <= 0
            intX = 0
            Me.TextBox2.Text = Nothing
            intX = NewRtbx.Text.ToString.Length
            Dim intArray(intX) As Integer
            Me.TextBox2.Text = Me.NewRtbx.Text
            For Each items As String In Me.NewRtbx.Text 'picks the bits from top to bottom
                intArray(intX) = items
                intX -= 1
            Next
            intX = NewRtbx.Text.ToString.Length
            Me.NewRtbx.Clear()
            For i As Integer = 1 To intX Step 1 'reverse the bits from bottom to top
                Me.NewRtbx.AppendText(intArray(i))
            Next
            Me.TextBox2.Text = Me.NewRtbx.Text
        Catch ex As Exception
        End Try
    End Sub
End Class.

No comments:

Post a Comment