Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How we can convert a Character to Binary and ASCII code.

Author: DevASP
Download Source Code : 622_ChangeBinary.zip

This article is about how you can covert a character to binary and ASCII code. This is a desktop application. Enter any character you want to get the binary and ASCII. or enter the binary to get its A

Steps you will do.

 

Start visual studio and create a new window application.Set the following properties of the form

 

Name = “Form1”

Text = “Change Binary”

StartPostion = “CenterScreen”

 Now place three text boxes and set their properties as below.

 

First text box control

 

Name = “txtChar”

MaxLength = “1”

 

 

Second text box control

 

Name = “txtAscii”

Locked = “True”

ReadOnly = “True”

 

 

Third text box control

 

Name = “txtBinary”

Locked = “True”

ReadOnly = “True”

 

Now place three label controls on the form and set their properties as below.

 

 

First label control

 

Name = “Label1”

Text = “Character :”

TextAlign = “MiddleRight”

 

 

Second label control

 

Name = “Label2”

Text = “Ascii Code ::”

TextAlign = “MiddleRight”

 

 

Third label control

 

Name = “Label3”

Text = “Binary Code :”

TextAlign = “MiddleRight”

 

 

Now place a button on the form and set its following properties.

 

Name = “cmdClose”

Text = “Close”

FlatStyle = “Popup”

Cursor = “Hand”

 

 

Open the code window of form and write the following code in the text change event of the txtChar.

 

If txtChar.Text <> "" Then

            txtAscii.Text = Asc(txtChar.Text)

            txtBinary.Text = CLS.Cal_CharToBinary(txtChar.Text)

        Else

            txtAscii.Text = vbNullString

            txtBinary.Text = vbNullString

End If 

 

 

Write the following code in the click event of the cmdClose button to exit form the application.

 

End

 

 

Write following code in the text change event of the txtBinarry.

 

If Len(txtBinary.Text) = 8 Then

            txtAscii.Text = CLS.Cal_BinaryToAsci(txtBinary.Text)

            txtChar.Text = Chr(Val(txtAscii.Text))

        Else

            txtAscii.Text = ""

            txtChar.Text = ""

End If

 

 

Now declare a class in the code window and write the following functions in that as below.

 

 

Public Class CalculateFunctions

    Public Function Cal_CharToBinary(ByVal CharStr As String) As String

        Dim LastValue As Integer

        Dim BinaryArr() As String

        Dim BinaryStr As String

        Dim i As Integer

        LastValue = Asc(CharStr)

        i = 0

        ReDim BinaryArr(i)

        While LastValue <> 0

            ReDim Preserve BinaryArr(i)

            BinaryArr(i) = LastValue Mod 2

            LastValue = LastValue \ 2

            i = i + 1

        End While

        If UBound(BinaryArr) >= 0 Then

            For i = 0 To UBound(BinaryArr)

                BinaryStr = BinaryArr(i) & BinaryStr

            Next

            BinaryStr = String.Format("0", 8 - Len(BinaryStr)) & BinaryStr

        End If

        Cal_CharToBinary = BinaryStr

    End Function

 

    Public Function Cal_BinaryToAsci(ByVal BinaryStr As String) As Integer

        Dim LPos As Integer

        Dim tmpVal As Integer

        Dim i As Integer

        LPos = 0

        tmpVal = 0

        While LPos >= 0

            LPos = InStr(LPos + 1, BinaryStr, "1")

            If LPos > 0 Then

                tmpVal = tmpVal + (2 ^ (8 - LPos))

            Else

                LPos = -1

            End If

        End While

        Cal_BinaryToAsci = tmpVal

    End Function

End Class 

 

Run your application. Enter the character to get its binary and ASCII code or enter the binary number to get its ASCII code and the character.

Article Comments
i need String To Ascii code in V C ++
How i Convert String to ASCII conversion in vc++.

Posted on 1/15/2007 7:05:49 AM by shahid

How can i prevent users to enter letters in a text box. I want to just let the user enter numbers?

Posted on 8/8/2007 5:52:13 AM by wissam

Hello, it code is for visual version???
I need for Visual Basic 6.0.

Posted on 9/4/2007 5:23:29 PM by carolina mateus

Is good.......... i hope with this....
How we can convert a binary to Character and ASCII code.

Posted on 10/3/2007 8:32:18 AM by velmurugan

Is good.......... i hope with this....
How we can convert a binary to Character
pls....pls...give me the code in vb

Posted on 10/3/2007 8:33:05 AM by velmurugan

I want to retrieve the image in Datalist in ASP.NET(VB) from SQL Server 2005.Could You Please Help........

Posted on 12/16/2007 6:07:11 AM by Vipin Arora

How to retrieve Image in Datalist in ASP.NET

Posted on 12/19/2007 5:02:34 AM by Vipin

can u send me help about how to convert text string to binary using asp.net

Posted on 12/24/2007 3:07:19 AM by monika

That's great and all, but the Asc() function only converts the first character in a string.

What this means is that if my input string is "hello world!", the ASCII value that will be returned will be 104 because that's the ASCII value of "h"

Posted on 7/28/2010 2:47:08 AM by Logan

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Generate Backup Folder Which Monitor a Folder and create its Backup C#.net application in .NET 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net