Search from Cycling Blog

Converting decimal to binary[VB.NET]

Converting decimal number into Binary number

Language: VB.NET

Author: Aamir Mustafa

E-Mail: aamir_mustafa2007@msn.com

aamirmustafa134@gmail.com

Introduction:

This article will show you how to convert a decimal number into binary number.

How to Use:

Create an empty project

On the form, add a button, two textboxes

  1. btnBinary
  2. txtNum
  3. txtBinary

We will take input through txtNum and show binary number into txtBinary and we will use btnBinary for converting decimal into binary.

Write following code in btnBinray Click event.

Code:

Private Sub btnBinary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBinary.Click

Try

Dim num As Long 'number that we will take through input

num = CLng(txtNum.Text) 'assigning textbox value to num and converting string value to Long using CLng function

If IsNumeric(num) Then 'checking that text box contains numeric value

txtBinary.Text = Convert.ToString(num, 2) 'here 2 means converting to binary

End If

Catch ex As Exception

MessageBox.Show("Plz enter the number", "Binary", MessageBoxButtons.OK, MessageBoxIcon.Information)

txtNum.Focus()

End Try

End Sub

 

0 comments: